6

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

EnzoMolion
  • 949
  • 8
  • 25

2 Answers2

9

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
EnzoMolion
  • 949
  • 8
  • 25
  • 2
    And if you already have file org.mockito.plugins.MockMaker under src/test/resources/mockito-extensions, remember to delete it ! – Sann Tran Aug 13 '20 at 05:39
  • What about `pom.xml` for Maven? I could not find these folder and settings you mentioned above. Any help please? –  Mar 13 '22 at 09:34
4

I dont have mockito-inline and I have mock-maker.inline, my setup is org.powermock:powermock-api-mockito2:2.0.9 and org.mockito:mockito-core:3.10.0.

However I found that PowerMockito.verifyStatic is using Mockito.verify underneath and when the class is checked for isMock mockito uses wrong MockMaker and can not find the class in mockedStatics collection.

When I used

Mockito.mockStatic(MyClass.class);

instead of

PowerMockito.mockStatic(MyClass.class);

it started to work just fine for me.

EnzoMolion
  • 949
  • 8
  • 25
Radek Riedel
  • 311
  • 2
  • 3
  • Anyway I would agree if someone says my answer is nto 100% OK, as I just found a case where PowerMockito.mockStatic works with PowerMockito.verifyStatic, however I can not find where is the problem. I have debug the mockito quite deeps but ... at least my solution works where I need it – Radek Riedel Aug 11 '21 at 08:23
  • When I convert `PowerMockito.mockStatic(MyClass.class);` to `Mockito.mockStatic(MyClass.class);`, the error goene, but this time if cannot verify the hit(s) even my code hits one time and gives "*Wanted but not invoked:*" error. It just verify times(0) even it is 1 actually. Any idea? –  Mar 13 '22 at 09:37