Stubbing a method that takes Class as parameter with Mockito

后端 未结 3 690
迷失自我
迷失自我 2020-12-29 01:08

There is a generic method that takes a class as parameter and I have problems stubbing it with Mockito. The method looks like this:

public 

        
3条回答
  •  天命终不由人
    2020-12-29 01:41

    Nice one @Ash. I used your generic class matcher to prepare below. This can be used if we want to prepare mock of a specific Type.(not instance)

    private Class streamSourceClass() {
        return Mockito.argThat(new ArgumentMatcher>() {
    
            @Override
            public boolean matches(Object argument) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }
    

    Usage:

        Mockito.when(restTemplate.getForObject(Mockito.anyString(), 
                **streamSourceClass(),**
                Mockito.anyObject));
    

提交回复
热议问题