Search code examples
javamockitopowermockpowermockito

Powermock: NoClassDefFoundError when trying to mock static classes


I'm trying to understand how to use Powermock. I'm trying to realize the example of Static method mocking here.

I created this code based on the example above.

I however get a NoClassDefFoundError when trying to run the test.

I don't know what exactly is causing this error as it is mostly copy pasted code.

// imports redacted

@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class)
public class YourTestCase {
    @Test
    public void testMethodThatCallsStaticMethod() throws Exception {
        // mock all the static methods in a class called "Static"
        PowerMockito.mockStatic(Static.class);
        // use Mockito to set up your expectation
        PowerMockito.when(Static.class, "firstStaticMethod", any()).thenReturn(true);
        PowerMockito.when(Static.class, "secondStaticMethod", any()).thenReturn(321);

        // execute your test
        new ClassCallStaticMethodObj().execute();

        // Different from Mockito, always use PowerMockito.verifyStatic() first
        // to start verifying behavior
        PowerMockito.verifyStatic(Mockito.times(2));
        // IMPORTANT:  Call the static method you want to verify
        Static.firstStaticMethod(anyInt());


        // IMPORTANT: You need to call verifyStatic() per method verification,
        // so call verifyStatic() again
        PowerMockito.verifyStatic(); // default times is once
        // Again call the static method which is being verified
        Static.secondStaticMethod();

        // Again, remember to call verifyStatic()
        PowerMockito.verifyStatic(Mockito.never());
        // And again call the static method.
        Static.thirdStaticMethod();
    }
}

class Static {
    public static boolean firstStaticMethod(int foo) {
        return true;
    }

    public static int secondStaticMethod() {
        return 123;
    }

    public static void thirdStaticMethod() {
    }
}

class ClassCallStaticMethodObj {
    public void execute() {
        boolean foo = Static.firstStaticMethod(2);
        int bar = Static.secondStaticMethod();
    }
}

Solution

  • PowerMock 1.6.6 seems to be incompatible with Mockito 2.7

    I've made some changes to your pom.xml. First I changed powermock version:

    <powermock.version>1.7.0RC2</powermock.version>
    

    Then I changed powermock-api-mockito to powermock-api-mockito2 (the first one didn't work, it seems to be incompatible with Mockito 2.7):

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    

    This solved the NoClassDefFoundError.


    Anyway, I still had to change this to make it work: instead of PowerMockito.when(), you should use Mockito.when():

    Mockito.when(Static.firstStaticMethod(anyInt())).thenReturn(true);
    Mockito.when(Static.secondStaticMethod()).thenReturn(321);