我一直面临一个特殊的问题。基本上,当我正常运行我的 Mockito 测试时,即“作为 Junit 测试运行”,它会给我以下错误。有人可以帮我吗我的错误是什么?
收到的错误:
java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;
at org.powermock.api.mockito.mockmaker.MockMakerLoader.doLoad(MockMakerLoader.java:45)
at org.powermock.api.mockito.mockmaker.MockMakerLoader.load(MockMakerLoader.java:36)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:36)
... shortened stacktrace....
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
测试类:
public class ListTest {
@Test
public void letsMockListSize() {
List<?> list= mock(List.class);
when(list.size()).thenReturn(2);
assertEquals(2, list.size());
}
}
pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
原文由 Krish 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您删除 PowerMock 依赖项,该示例将起作用。问题是所使用的 Mockito 和 PowerMock 的版本彼此不兼容。对于 PowerMock 2.x,您至少需要 Mockito 2.8.9+。 PowerMock 提供了一个 兼容性列表,显示了哪个版本的 PowerMock 与哪个版本的 Mockito 兼容。修复版本以相互兼容,您的示例将开始工作。