mockito测试在作为junit测试运行时没有给出这样的方法错误,但是当在运行混淆中手动添加jar时,它运行良好

2022-09-01 17:59:21

我一直面临着一个奇怪的问题。基本上,当我正常运行Mockito / PowerMockito测试时,即“运行为Junit Test”,它会给我以下错误:

java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.isUsingConstructor()Z
at org.mockito.internal.creation.instance.InstantiatorProvider.getInstantiator(InstantiatorProvider.java:10)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:203)
at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:106)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:54)
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我曾尝试在 maven 依赖项和构建路径中添加所有必需的 jar,但无济于事。

但是,我随后在运行配置>类路径>用户条目中手动添加了 jar。然后,我像往常一样运行测试,它显示绿色状态栏,即它运行良好。

请帮帮我。我需要在没有这些烦人的更改的情况下进行测试,这些更改会影响我的工作。我能做些什么来让我正常运行测试,即不一直运行配置,而只需在构建路径中添加jar,然后“作为junit测试运行”?


答案 1

在日食屏幕上尝试 Cntrl + Shift + T。检查您的结果。理想情况下,eclipse 应该仅从一个依赖项解析此类。如果获得多个结果,则可能需要对某些依赖项添加排除项。

enter image description here

还要检查您是否能够从命令提示符执行测试用例。如果你能够做到这一点,那么这不是一个专家问题。但是,如果此处出现问题,请检查您的依赖项。


答案 2

对我来说,mockito版本正在制造问题。只需删除 mockito 依赖项,如果您已在 pom 文件中显式添加。我刚刚删除了 -

testCompile 'org.mockito:mockito-all:1.9.5' 

行从我的 gradle 文件。让 power mock 解决并添加它自己需要的 mockito 依赖项,而不是强制添加一个可能导致冲突的依赖项。

所以对我来说,依赖关系是 -

testCompile "org.powermock:powermock-module-junit4:1.6.4"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.4" 
testCompile "org.powermock:powermock-api-mockito:1.6.4" 
testCompile "org.powermock:powermock-classloading-xstream:1.6.4" 

除了 junit 和您可能拥有的其他库依赖项。


推荐