[英]java.lang.NoSuchMethodError: org.mockito.internal.runners.RunnerFactory.createStrict(Ljava/lang/Class;)Lorg/mockito/internal/runners/InternalRunner;
I am writing my Junit test cases for Groovy using Mockito jar, but it is giving me following exception:我正在使用 Mockito jar 为 Groovy 编写 Junit 测试用例,但它给了我以下异常:
java.lang.NoSuchMethodError: org.mockito.internal.runners.RunnerFactory.createStrict(Ljava/lang/Class;)Lorg/mockito/internal/runners/InternalRunner;
at org.mockito.junit.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:152)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
For some reason, your test suite, tries to load the MockitoJunitRunner
from the org.mockito.junit
contained in the Mockito versions >= 2. O. In that version, the line: 出于某种原因,你的测试套件,试图加载
MockitoJunitRunner
从org.mockito.junit
包含在版本的Mockito> = 2 O.在该版本中,行:
at org.mockito.junit.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:152)
is doing this: 正在这样做:
public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
//by default, StrictRunner is used. We can change that potentially based on feedback from users
this(new StrictRunner(new RunnerFactory().createStrict(klass), klass));
}
and the RunnerFactory that is loaded here is from version 1.x as createStrict
has been introduced in Mockito 2.x. Mockito 2.x中引入了
createStrict
,因此此处加载的RunnerFactory来自1.x版本。
So go through the pom dependency tree and to find what artifact implicitly add the Mockito 2.x dependency to your project and exclude it. 因此,遍历pom依赖关系树,找到哪些工件将Mockito 2.x依赖项隐式添加到您的项目中并排除它。
Alternatively.. as a workaround, instead of the @RunWith(MockitoJUnitRunner.class)
you can use: 另外,作为解决方法,可以使用
@RunWith(MockitoJUnitRunner.class)
代替:
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
You can also check out this Mockito cheat sheet to keep all the standards at hand. 您也可以查看此Mockito备忘单,以掌握所有标准。
In Mockito 1.10.19 (which is from 2014), there is no class org.mockito.junit.MockitoJUnitRunner
. 在Mockito 1.10.19(从2014年开始)中,没有
org.mockito.junit.MockitoJUnitRunner
类。 This was introduced later in 2.x. 稍后在2.x中引入。 If you really want to use 1.10.19, you should probably use the then correct class
org.mockito.runners.MockitoJUnitRunner
which then should work. 如果您确实想使用1.10.19,则应该使用当时正确的
org.mockito.runners.MockitoJUnitRunner
类。
But I would strongly recommend using a newer Mockito version instead. 但我强烈建议您使用较新的Mockito版本。
mockito-all
is simply not the right artifact to depend on anymore. mockito-all
根本不是正确的工件。 With 2.x this artefact was not maintained anymore. 使用2.x时,不再保留此伪像。
The problem lies within your imports. 问题在于您的进口。 Your imports doesn't include import runner replace the following import
您的导入不包括导入运行器,请替换以下导入
org.mockito.junit.MockitoJUnitRunner
with 与
org.mockito.runners.MockitoJUnitRunner;
i ran into the same issue with following versions.我在以下版本中遇到了同样的问题。 In my case there was a
mockito-core
dependency through org.apache.maven:maven:3.6.3
.在我的例子中,通过
org.apache.maven:maven:3.6.3
有一个mockito-core
依赖。 So i had to override the dependency.所以我不得不覆盖依赖。
<version.maven-surefire-plugin>3.0.0-M5</version.maven-surefire-plugin>
<version.junit-jupiter-engine>5.8.2</version.junit-jupiter-engine>
<version.mockito-junit-jupiter>4.3.1</version.mockito-junit-jupiter>
to check this, you can use: mvn help:effective-pom -Dverbose=true -Doutput=effective-pom.xml
要检查这一点,您可以使用:
mvn help:effective-pom -Dverbose=true -Doutput=effective-pom.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.