When I'm testing this static method
public class SomeClass {
public static long someMethod(Map map, String string, Long l, Log log) {
...
}
}
with
import org.apache.commons.logging.Log;
@RunWith(PowerMockRunner.class)
//@PrepareForTest(SomeClass.class)
public class Tests {
@Test
public void test() {
...
PowerMockito.mockStatic(SomeClass.class);
Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L);
...
}
}
I got InvalidUseOfMatchersException
. My questions are:
isA(Log.class)
returns null.@PrepareForTest
annotation to the test class and run the test, the junit makes no response. Why?EDIT
I tried not to use argument matchers, and got
org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified.
inside when() you don't call method on mock but on some other object.
at ...
So it seems due to the someMethod
itself. There are synchronized block in the method. I'm wondering if Powermockito can mock that kind of method or not.
InvalidUseOfMatchersException: Invalid use of argument matchers! 2 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.
Because there is no instance variable, the class name itself should be used to access the members of a static class. The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results.
While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
Try replacing the isA() to another any() call like this
Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), any(Log.class))).thenReturn(1L);
[EDIT]
Check your stacktrace when you get the exception. Are you seeing any NoClassDefFoundError
reported? I noticed when I hadn't included the javassist.jar
in my project I got a similar error to you.
I use PowerMockito and these are the jars I added to a brand new project to run the code that @Tom posted
Always a good idea to check that you're using compatible JAR versions, and also check if there are any other conflicting JARs in your projects classpath.
Better late than never, the line below:
Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(),
isA(Log.class))).thenReturn(1L);
should be:
PowerMockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(),
isA(Log.class))).thenReturn(1L);
So, instead of invoking Mockito.when
, one should invoke PowerMockito.when
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With