I have mock method for a rest call.
public void createRestClientMock() {
org.mockito.Mockito.when(
restClient.sendResponse(org.mockito.Matchers.anyString(),
org.mockito.Matchers.anyString())).thenAnswer(
new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
actualResponse = args[1].toString();
LOG.debug(actualResponse);
return null;
}
});
}
The real method is getting mocked as expected and everything is working fine when i run the junit from eclipse.
But when i try to run the junit from Jenkins using maven build, i am getting the below error:-
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at uk.co.common.Test.createRestClientMock(Test.java:166)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
adding the dependencies
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.13-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>