我使用的是JUnitParams
,junit
和mockito-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'
在运行带注释NoSuchMethodError
的JUnit测试时,我不断获得@RunWith(MockitoJUnitRunner.class)
。
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");
}
}
答案 0 :(得分:0)
你的类路径上有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
...