gpt4 book ai didi

java - NoSuchMethodError : org. mockito.internal.runners.RunnerFactory.createStrict

转载 作者:太空宇宙 更新时间:2023-11-04 10:27:25 26 4
gpt4 key购买 nike

我正在使用最新版本的 JUnitParamsjunitmockito-all

testCompile group: 'pl.pragmatists', name: 'JUnitParams', version: '1.1.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'

使用注释 @RunWith(MockitoJUnitRunner.class) 运行 JUnit 测试时,我不断收到 NoSuchMethodError

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:154)
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 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

什么问题可能会触发 NoSuchMethodError 异常?

代码:

@RunWith(MockitoJUnitRunner.class)
public class MockTest {

@Mock
ServletUriComponentsBuilder servletUriComponentsBuilder;

@Before
public void setup() {
servletUriComponentsBuilder = mock(ServletUriComponentsBuilder.class);
}

@Test
public void shouldGenerateUrl() {
when(servletUriComponentsBuilder.fromUriString(anyString()).build().toString())
.thenReturn("test");

Assert.assertEquals("my message", "test", "test");
}

}

最佳答案

mockito-core

您的类路径上有 Mockito 1.x 和 2.x。我认为这是因为您依赖于 mockito-all:2.0.2-beta 并且您的一些其他依赖项具有对 mockito-core:1.x 的传递依赖。

您永远不应该将 mockito-all 与进行依赖项管理的构建系统一起使用。而是使用

testCompile group: 'org.mockito', name: 'mockito-core', version: '2.0.2-beta'

甚至是更新的版本(请参阅 The Central Repository )

Mockito 文档:https://github.com/mockito/mockito/wiki/Declaring-mockito-dependency

如果这不能解决问题,请告诉我。

创建模拟

这不是问题的原因,但可能也有帮助。您将创建 ServletUriComponentsBuilder 两次。首先,MockitoRunner 由于 @Mock 注释而创建它,然后您在 setup 方法中使用新的模拟覆盖它。所以要么你这样做

@RunWith(MockitoJUnitRunner.class)
public class MockTest {

@Mock
ServletUriComponentsBuilder servletUriComponentsBuilder;

@Test
...

或者你这样做

public class MockTest {

ServletUriComponentsBuilder servletUriComponentsBuilder;

@Before
public void setup() {
servletUriComponentsBuilder = mock(ServletUriComponentsBuilder.class);
}

@Test
...

你甚至可以做得更短

public class MockTest {

ServletUriComponentsBuilder servletUriComponentsBuilder
= mock(ServletUriComponentsBuilder.class);

@Test
...

关于java - NoSuchMethodError : org. mockito.internal.runners.RunnerFactory.createStrict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50387577/

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