我一直面临着一个特殊的问题。基本上,当我正常运行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());
}
}
<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>
答案 0 :(得分:1)
如果删除PowerMock依赖项,该示例将起作用。问题是所使用的Mockito和PowerMock的版本彼此不兼容。对于PowerMock 2.x,您至少需要Mockito 2.8.9+。 PowerMock提供了compatibility list,它显示了哪个版本的PowerMock与哪个版本的Mockito兼容。修复版本以使其相互兼容,您的示例将开始工作。