0
@Component
class ClassA{

    @Autowired
    ClassB classB;
     public void doSomething(){
         classD.createValues(a,b);
         //create values calls ClassB method
     }
}

@Component
class ClassB{

    @Autowired
    DynamoDBMapper mapper;

    public void doSomething(){
        mapper.scan(classC.class,new DynamoDBScanExpression()).stream();
    }

}  

Test Class

 @RunWith(SpringJUnit4ClassRunner.class)
 class TestClass{

    @InjectMocks
    @Autowired
    ClassA classA;

    @Mock 
    ClassD classD;

    @Autowired
    @Qualifier("dynamodbMapper")
    private DynamoDBMapper mockedDynamoDBMapper;
    // globally mocked in config

        @Test
        public void testWithValidData() {
            A a = new A();
            B b = new B();
            setUp(classA);
            mockDynamoDBCall();
            classA.doSomthing();
        }

           private void setUp(ClassA classA){
               Mockito.when(classD.createValues(a,b)).thenReturn(Matchers.any(Reponse.class)); // problem after mockDynamoDBCall()
           }
           private void mockDynamoDBCall(){
               when(mapper.scan(Mockito.eq(Object.class), Mockito.any(DynamoDBScanExpression.class))).
               thenReturn(mockPaginatedScanList);
               when(mockPaginatedScanList.stream()).thenReturn(createDummyData().stream());
           }
 }

when I was not mocking DynamoDBMapper its working fine.

After mocking DynamoDB mapper it is throwing exception in setUp method

[junit]     Caused an ERROR
[junit]
[junit] Invalid use of argument matchers!
[junit] 2 matchers expected, 1 recorded:
[junit] -> at  <class name>
[junit]
[junit] This exception may occur if matchers are combined with raw values:
[junit]     //incorrect:
[junit]     someMethod(anyObject(), "raw String");
[junit] When using matchers, all arguments have to be provided by matchers.
[junit] For example:
[junit]     //correct:
[junit]     someMethod(anyObject(), eq("String by matcher"));
[junit]
[junit] For more info see javadoc for Matchers class.
[junit]
[junit] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[junit] Invalid use of argument matchers!
[junit] 2 matchers expected, 1 recorded:
[junit] -> at <class name>
[junit]
[junit] This exception may occur if matchers are combined with raw values:
[junit]     //incorrect:
[junit]     someMethod(anyObject(), "raw String");
[junit] When using matchers, all arguments have to be provided by matchers.
[junit] For example:
[junit]     //correct:
[junit]     someMethod(anyObject(), eq("String by matcher"));

I tried passing Matchers.any(ClassName) and Matcher.any() as arguments but still i am getting same exception

pvpkiran
  • 25,582
  • 8
  • 87
  • 134

1 Answers1

2

This line

 Mockito.when(classD.createValues(a,b)).thenReturn(Matchers.any(Reponse.class));

makes no sense. You have to tell Mockito what to return. You can't just tell it to return any Response.class. This isn't what matchers do.

Matchers are used for checking the arguments that are passed to a method. They can't be used after thenReturn.

If you fix this up, the error will go away.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I know but before adding mockDynamoDBCall() why it was working fine? Any idea? – Prabhakar Yadav May 11 '17 at 09:54
  • Yes, all the methods that create a `Matcher` actually put the `Matcher` on Mockito's own internal stack, waiting for a stubbing or verify call. If you don't make a stubbing or verify call, Mockito can't detect the error. – Dawood ibn Kareem May 11 '17 at 10:14
  • can you provide me any doc regarding that? – Prabhakar Yadav May 11 '17 at 10:26
  • I don't think the Mockito team have explicitly documented this, at least not for public consumption. It's really just an implementation detail of Mockito itself; and the team may want to change the implementation at some point in the future. But the bottom line is that if you use the Mockito matchers correctly, following the documentation, and never do any matching where _some_ arguments are matchers and some are not; then you won't ever get that error. – Dawood ibn Kareem May 11 '17 at 19:18
  • Read [Jeff Bowman's excellent self-answer here](http://stackoverflow.com/q/22822512) – Dawood ibn Kareem May 11 '17 at 19:19