gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:15 35 4
gpt4 key购买 nike

我正在使用 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)

下面是我拥有的 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

下面是我的代码。我添加了必要的导入:

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)
}


}

最佳答案

出于某种原因,您的测试套件尝试从 Mockito 版本 >= 2.O 中包含的 org.mockito.junit 加载 MockitoJunitRunner。版本,行:

at org.mockito.junit.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:152)

正在这样做:

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));
}

并且此处加载的 RunnerFactory 来自版本 1.x,因为 createStrict 已在 Mockito 2.x 中引入。

因此,通过 pom 依赖关系树并找到哪些工件将 Mockito 2.x 依赖关系隐式添加到您的项目并将其排除。

或者.. 作为一种解决方法,您可以使用以下方法代替 @RunWith(MockitoJUnitRunner.class):

@Before
public void init() {
MockitoAnnotations.initMocks(this);
}

您还可以查看此 Mockito cheat sheet随时掌握所有标准。

关于java.lang.NoSuchMethodError : org. mockito.internal.runners.RunnerFactory.createStrict(Ljava/lang/Class;)Lorg/mockito/internal/runners/InternalRunner;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47652324/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com