Search code examples
testngcouchbasepowermockito

PowerMockito not working : -java.lang.NoClassDefFoundError: org/mockito/internal/creation/CglibMockMaker


I am trying to mock the static method of CouchbaseCluster.create() using powermockito. Here is my test class.

@PrepareForTest(CouchbaseCluster.class)
public class IAMKafkaConsumerTest extends PowerMockTestCase {
    
    private IAMKafkaConsumer iamKafkaConsumer;
    private CouchbaseCluster mockCouchbaseCluster;
    private Bucket mockBucket;
    
    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }

    @Test
    public void happyPath()
    {
        PowerMockito.mockStatic(CouchbaseCluster.class);
       
    }
    
}

My project pom includes the following dependencies:

<dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <scope>test</scope>
        </dependency> 
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <scope>test</scope>
        </dependency>
          <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-testng</artifactId>
       <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-testng-agent</artifactId>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <scope>test</scope>
      </dependency>
       <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <scope>test</scope>
    </dependency>
        
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>
    

I am getting this error log when I run the test through TestNG framework.

java.lang.NoClassDefFoundError: org/mockito/internal/creation/CglibMockMaker at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.(PowerMockMaker.java:40) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at java.lang.Class.newInstance(Class.java:379) at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:61) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)


Solution

  • It turns out that PowerMockito 1.5.X version is broken when used with mockito 1.10.9

    Therefore changing the version to 1.6.X works for me. This link helped me.

    https://code.google.com/p/powermock/issues/detail?id=524