java.lang.NoSuchMethodError: org.mockito.internal.runners.RunnerFactory.createStrict(Ljava/lang/Class;)Lorg/mockito/internal/run

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47652324/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:39:59  来源:igfitidea点击:

java.lang.NoSuchMethodError: org.mockito.internal.runners.RunnerFactory.createStrict(Ljava/lang/Class;)Lorg/mockito/internal/runners/InternalRunner;

javaunit-testinggroovyjunitmockito

提问by LowCool

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)

Below is the jar list that I have:

以下是我拥有的 jar 列表:

cglib-nodep-2.2.2
javassist-3.19.0-GA
junit-4.12
mockito-all-1.10.19
objenesis-2.5
powermock-mockito-1.6.2-full

Below is my code. I have added necessary imports:

下面是我的代码。我添加了必要的进口:

package test.service
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
class SyncImplTest {

    @InjectMocks
    SyncThreatImpl  fixture;

    @Mock
    RpcConfigurationLoader rpcConfigurationLoader

    @Test
    public void testRpcConfig(){
        RpcApiInfo rpcApiInfo =  new RpcApiInfo();
        when(rpcConfigurationLoader.loadConfiguration()).thenReturn(rpcApiInfo)
    }


}

采纳答案by Maciej Kowalski

For some reason, your test suite, tries to load the MockitoJunitRunnerfrom the org.mockito.junitcontained in the Mockito versions >= 2. O. In that version, the line:

出于某种原因,您的测试套件尝试MockitoJunitRunnerorg.mockito.junitMockito 版本 >= 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 createStricthas been introduced in Mockito 2.x.

此处加载的 RunnerFactory 来自createStrictMockito 2.x 中引入的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 sheetto keep all the standards at hand.

您还可以查看此Mockito 备忘单以掌握所有标准。

回答by Vampire

In Mockito 1.10.19 (which is from 2014), there is no class org.mockito.junit.MockitoJUnitRunner. This was introduced later in 2.x. If you really want to use 1.10.19, you should probably use the then correct class org.mockito.runners.MockitoJUnitRunnerwhich then should work.

在 Mockito 1.10.19(从 2014 年开始)中,没有 class org.mockito.junit.MockitoJUnitRunner。这是后来在 2.x 中引入的。如果您真的想使用 1.10.19,您可能应该使用org.mockito.runners.MockitoJUnitRunner当时应该可以工作的正确类。

But I would strongly recommend using a newer Mockito version instead. mockito-allis simply not the right artifact to depend on anymore. With 2.x this artefact was not maintained anymore.

但我强烈建议改用较新的 Mockito 版本。mockito-all根本就不再是可以依赖的正确神器。在 2.x 中,这个人工制品不再被维护。

回答by malvern dongeni

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;