Ambiguous Mockito - 0 expected matches, 1 record (InvalidUseOfMatchersException)

I ran into a very strange problem.

URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum), eq(this.config.apiEndpoint() + URL), any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent)); 

or even with contains

  URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum), contains(this.config.apiEndpoint() + URL), any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent)); 

gives me

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

Even if I do not use RAW expressions.
Oddly enough, if I changed the contains method to:

 URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum), contains(URL), any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent)); 

dropping the endpoint, it works.

Configuration and RestHelper both mock:

 this.restHelperMock = mock(RESTHelper.class); this.config = mock(MyBMWConfiguration.class); when(this.config.apiEndpoint()).thenReturn("http://host:port/api"); 

The URL with ApiEndpoint is equal to what I wanted to make fun of, even if it weren’t, I should get a NullpointerException, due to false bullying. But here I have no ideas.

Thank you for your responses.

+5
source share
1 answer

The problem is that you are calling the this.config.apiEndpoint() method of bullying while calling eq ( ... ) . Try just putting the full URL there (host: port / api / my / specific / url) instead of calling another layout that might confuse Mockito as it relies on internal states for ridicule.

Honestly, I'm not so deep in Mokkito that I can explain WHY this is happening, but I will probably try to debug it once ,-)

Edit: Oddly enough, I don't seem to be able to play it with a simpler test recording. It seems that there is more than it seems at first glance.

+4
source

Source: https://habr.com/ru/post/1234257/


All Articles