3

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)

4

2 回答 2

5

事实证明,PowerMockito 1.5.X 版本在与 mockito 1.10.9 一起使用时被破坏

因此将版本更改为 1.6.X 对我有用。这个链接帮助了我。

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

于 2015-07-13T18:44:54.900 回答
1

除了 Puneet 回答之外,您的项目中总是有可能拥有多个版本的 powermock lib,尤其是当它是一个巨大的版本时。就我而言,我通过项目文件搜索“powermock”关键字,发现powermock-mockito-release-full-1.6.1-full.jarpowermock-api-mockito-1.7.0.jar都有PowerMockMaker类。因此,删除不兼容的 1.6.1 版本解决了这个问题。

于 2021-05-11T16:42:45.343 回答