PowerMockito: got an InvalidUseOfMatchersException when using matches mocking a static method

When I test this static method

public class SomeClass { public static long someMethod(Map map, String string, Long l, Log log) { ... } } 

from

 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 an InvalidUseOfMatchersException . My questions:

  • Why did I get this exception when all arguments use matches? How to solve it? I debugged it, found that isA(Log.class) returns null.
  • When I add the @PrepareForTest annotation to the test class and run the test, junit is not responding. Why?

EDIT

I tried not to use argument arguments and got

org.mockito.exceptions.misusing.MissingMethodInvocationException: when () requires an argument, which should be a "method call to the layout". For example: when (mock.getArticles ()) thenReturn (articles) ;.

In addition, this error may appear because:

  • you drown either of the following: final / private / equals () / hashCode (). These methods cannot be erased / verified.

  • inside when () you do not call the method on mock, but on some other object.

at ...

So it seems, because of someMethod itself. The method has a synchronized block. I am wondering if Powermockito can mock this method or not.

+4
source share
4 answers

Try replacing isA () with another any () call like this

 Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), any(Log.class))).thenReturn(1L); 

[EDIT]

Check your stack when you get an exception. Do you see a NoClassDefFoundError message? I noticed that when I did not include javassist.jar in my project, I had a similar error.

I use PowerMockito and these are the banks that I added to a completely new project to run the code that @Tom sent

  • powermock-mockito-1.4.10-full.jar
  • Mockito-all-1.8.5.jar
  • Javassist-3.15.0-GA.jar
  • JUnit-4.8.2.jar
  • total logging 1.1.1.jar

It is always a good idea to check that you are using compatible versions of the JAR, and also to check if there are any other conflicting JARs in the path to the project classes.

+2
source
  • isA will always return null . This is by design, it is described in Javadoc for the isA () method. The reason for this is because the null value will always match the parameterized type of the return class, which will always match the argument type in the stubbed method for which the isA () method is used. The return null value is actually not valid.

  • Everything seems to be in order. My complete test case:

 import static org.mockito.Matchers.*; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.impl.SimpleLog; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; class SomeClass { public static long someMethod(final Map map, final String string, final Long l, final Log log) { return 2L; } } @RunWith(PowerMockRunner.class) @PrepareForTest(SomeClass.class) public class InvalidUseOfMatchersTest { @Test public void test() { // Mock the SomeClass' static methods, stub someMethod() to return 1 PowerMockito.mockStatic(SomeClass.class); Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); // null NOT is-a Log, uses default stubbing: returns 0 System.out.println(SomeClass.someMethod(null, null, 5L, null)); // SimpleLog passes is-a test, uses stubbed result: returns 1 System.out.println(SomeClass.someMethod(null, null, 7L, new SimpleLog("simplelog"))); } } 

Perhaps post a complete example to help diagnose what is happening?

0
source

Better late than never, line below:

 Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); 

it should be:

 PowerMockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); 

So, instead of calling Mockito.when should call PowerMockito.when

0
source
 <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>${mockito.version}</version> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>${powermock.version}</version> <type>jar</type> <scope>provided</scope> </dependency> 

I hope your project uses maven. Try to include these banks in the assembly.

-1
source

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


All Articles