I have a class that is a subclass Context. I am testing units of another class that are related to this class, and therefore I mocked it. However, I need some methods to act as their original behavior, so I'm going to βuntieβ them.
One of them getAssets(), so I wrote this and it works correctly:
Mockito.doReturn(this.getContext().getAssets()).when(keyboard).getAssets();
keyboard is a mocked instance of the class.
Since this method takes no parameters, redefining it was quite simple.
I also need to override Context.getString(int). The parameter complicates the situation and is primitive, which further complicates the work.
I took this advice and one more, and tried to write this code:
Mockito.when(keyboard.getString(Mockito.anyInt())).thenAnswer(new Answer<String>(){
@Override
public String answer(InvocationOnMock invocation) throws Throwable
Integer arg = (Integer)invocation.getArguments()[0];
return OuterClass.this.getContext().getString(arg.intValue());
}
});
This is compiled and executed, but has the following exception:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at [...] <The code above>
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.
at android.content.Context.getString(Context.java:282)
at [...]
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
So, the main question: how to override methods in Mockito that have primitive parameters?
Thanks in advance