Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotAMockException when trying to verify a static method

I am trying to verify that a static method is not called in a certain configuration, in a unit test.

I am thus using PowerMock (powermock-core:2.0.4 & powermock-module-junit4:2.0.4) and its Mockito API (powermock-api-mockito2:2.0.4).

When doing

PowerMockito.mockStatic(MyClass.class);

serviceUnderTest.methodThatShouldNotCallStaticMethod(arg1, arg2); //service not of type MyClass of course

PowerMockito.verifyStatic(MyClass.class, never());
MyClass.staticMethod(any(), any());

on a test method within a class annotated with

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})

I get the following error : org.mockito.exceptions.misusing.NotAMockException: Argument passed to verify() is of type Class and is not a mock!.

What did I do wrong and how to solve it ?

Thanks

like image 215
EnzoMolion Avatar asked Nov 07 '19 16:11

EnzoMolion


1 Answers

It turned out to be a Powermock bug...

Here is the workaround for anyone who could be interested :

  1. Delete line testImplementation 'org.mockito:mockito-inline:2.13.0 " in gradle
  2. Create a src\test\resources\org\powermock\extensions\configuration.properties file composed of the single line mockito.mock-maker-class=mock-maker-inline
like image 195
EnzoMolion Avatar answered Oct 31 '22 00:10

EnzoMolion