@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