Java 当作为 junit 测试运行时,mockito 测试不会给出这样的方法错误,但是当在运行配置中手动添加 jars 时,它运行良好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31962003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mockito test gives no such method error when run as junit test but when jars are added manually in run confugurations, it runs well
提问by Siddharth Mehta
I've been facing a peculiar problem. Basically, when I run my Mockito/PowerMockito test normally i.e. 'Run as Junit Test', it gives me the following error :
我一直面临一个奇怪的问题。基本上,当我正常运行 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.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)
I had tried adding all the required jars in the maven dependency as well as in the build path but to no avail.
我曾尝试在 maven 依赖项以及构建路径中添加所有必需的 jar,但无济于事。
However, I then added the jars manually in run configurations->classpath->user entries. Then, I ran the test as I normally would and it displayed the green status bar i.e. it ran perfectly.
但是,我随后在运行配置-> 类路径-> 用户条目中手动添加了 jar。然后,我像往常一样运行了测试,它显示了绿色状态栏,即它运行得很完美。
Please help me on this. I need to have the test run without these annoying changes which would affect my work. What could I do so that I cun run the test normally i.e. without going all the way to run configurations but simply by adding the jars in the build path and then 'run as junit test'?
请帮我解决这个问题。我需要在没有这些烦人的更改的情况下运行测试,这些更改会影响我的工作。我该怎么做才能正常运行测试,即无需一路运行配置,而只需在构建路径中添加 jar,然后“作为 junit 测试运行”?
采纳答案by asg
Try Cntrl + Shift + T on your eclipse screen. Check what are your results. Ideally eclipse should resolve this class only from one dependency. If you are getting multiple results, you may need to add exclusion on certain dependencies.
在 eclipse 屏幕上尝试 Cntrl + Shift + T。检查你的结果是什么。理想情况下,eclipse 应该只从一个依赖项解析这个类。如果您得到多个结果,您可能需要在某些依赖项上添加排除项。
Also check if you are able to execute your test cases from command prompt. If you are able to do it, then it's not a maven problem. But if something is failing here, check your dependencies.
还要检查您是否能够从命令提示符执行测试用例。如果您能够做到,那么这不是 Maven 问题。但是如果这里出现问题,请检查您的依赖项。
回答by Nick Holt
Your IDE needs to be synchronized with Maven, which is what you're doing manually by adding the dependencies.
您的 IDE 需要与 Maven 同步,这是您通过添加依赖项手动执行的操作。
Unfortunately Eclipse doesn't (or didn't) provide this behavior out-of-the-box (unlike IntelliJ which synchronizes seamlessly).
不幸的是,Eclipse 没有(或没有)提供这种开箱即用的行为(与无缝同步的 IntelliJ 不同)。
The solution used to be done with the Maven Eclipse Pluginbut looking at the docs it seems the latest way to do this is with Eclipse Maven Integrationplugin for the IDE.
该解决方案曾经使用Maven Eclipse 插件完成,但查看文档似乎最新的方法是使用IDE 的Eclipse Maven 集成插件。
回答by Aniket Thakur
For me mockito version was creating issues. Just remove mockito dependency if you have added explicitly in your pom file. I just removed -
对我来说,mockito 版本正在制造问题。如果您已在 pom 文件中明确添加,只需删除 mockito 依赖项。我刚刚删除 -
testCompile 'org.mockito:mockito-all:1.9.5'
line from my gradle file. Lets power mock resolve and add mockito dependency it needs on it's own instead of you forcefully adding one which may led to conflict.
我的 gradle 文件中的一行。让 power mock 解决并添加它自己需要的 mockito 依赖项,而不是你强行添加一个可能导致冲突的依赖项。
So for me dependencies are -
所以对我来说依赖是 -
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"
other than junit and other lib dependencies you may have.
除了junit和您可能拥有的其他lib依赖项。
回答by Pravind Kumar
I think there is an issue with Mockito version - I just updated the code from -
我认为 Mockito 版本存在问题 - 我刚刚更新了代码 -
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.8.5'
to
到
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'