PowerMockito not working : -java.lang.NoClassDefFoundError: org/mockito/internal/creation/CglibMockMaker
PowerMockito not working : -java.lang.NoClassDefFoundError: org/mockito/internal/creation/CglibMockMaker
我正在尝试使用 powermockito 模拟 CouchbaseCluster.create() 的静态方法。这是我的测试 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);
}
}
我的项目pom包括以下依赖:
<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>
当我 运行 通过 TestNG 框架进行测试时,我收到此错误日志。
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)
事实证明,PowerMockito 1.5.X 版本在与 mockito 1.10.9 一起使用时损坏
因此将版本更改为 1。6.X 对我有用。
这 link 帮助了我。
除了 Puneet 的回答,你的项目中总是有可能有多个版本的 powermock lib,尤其是当它很大的时候。在我的例子中,我通过项目文件搜索 'powermock' 关键字并找到 powermock-mockito-release-full-1.6.1-full.jar 和 powermock-api-mockito-1.7.0.jar,都有PowerMockMakerclass。因此,删除不兼容的 1.6.1 版本解决了这个问题。
我正在尝试使用 powermockito 模拟 CouchbaseCluster.create() 的静态方法。这是我的测试 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);
}
}
我的项目pom包括以下依赖:
<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>
当我 运行 通过 TestNG 框架进行测试时,我收到此错误日志。
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)
事实证明,PowerMockito 1.5.X 版本在与 mockito 1.10.9 一起使用时损坏
因此将版本更改为 1。6.X 对我有用。 这 link 帮助了我。
除了 Puneet 的回答,你的项目中总是有可能有多个版本的 powermock lib,尤其是当它很大的时候。在我的例子中,我通过项目文件搜索 'powermock' 关键字并找到 powermock-mockito-release-full-1.6.1-full.jar 和 powermock-api-mockito-1.7.0.jar,都有PowerMockMakerclass。因此,删除不兼容的 1.6.1 版本解决了这个问题。