java - mockito test 在作为 junit test 运行时没有给出这样的方法错误,但是当在运行配置中手动添加 jar 时,它运行良好

标签 java maven junit mockito powermock

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

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 测试运行”?

最佳答案

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

enter image description here

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

关于java - mockito test 在作为 junit test 运行时没有给出这样的方法错误,但是当在运行配置中手动添加 jar 时,它运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962003/

相关文章:

java - 两个Java类的比较

java - JDBC PreparedStatement 始终返回 1 作为自动生成的键

java - PostgreSQL 应用访问

eclipse - Eclipse 中缺少 ‘maven package’ 菜单项

java - Gradle 无法在 java 和 groovy 中运行测试

eclipse - Eclipse 中junit 的等宽字体已弃用

java - OSGi解决可插拔架构

java - 为什么我的 Android Studio 项目无法通过 Activity 打开?

java - Junit 测试失败但构建成功(Ant)

java - IntelliJ : automatically add current project-version to new class's comment