Reputation: 913
I am using latest versions of JUnitParams
, junit
and 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'
I keep getting NoSuchMethodError
when running a JUnit test with annotation @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)
What can be the problem that triggers NoSuchMethodError
exception?
Code:
@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");
}
}
Upvotes: 3
Views: 11341
Reputation: 24540
You have Mockito 1.x and 2.x on your classpath. I think that is because you have a dependency to mockito-all:2.0.2-beta
and some of your other dependencies has a transitive dependency to mockito-core:1.x
.
You should never use mockito-all
with a build system that does your dependency management. Instead use
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.0.2-beta'
or even a more current version (see The Central Repository)
Mockito documentation: https://github.com/mockito/mockito/wiki/Declaring-mockito-dependency
Please tell me, if this does not solve the problem.
This is not the cause of your problem but maybe helpful, too. You are creating the ServletUriComponentsBuilder
twice. First the MockitoRunner
creates it because of the @Mock
annotation and then you override it with a new mock in you setup
method. So either you do
@RunWith(MockitoJUnitRunner.class)
public class MockTest {
@Mock
ServletUriComponentsBuilder servletUriComponentsBuilder;
@Test
...
or you do
public class MockTest {
ServletUriComponentsBuilder servletUriComponentsBuilder;
@Before
public void setup() {
servletUriComponentsBuilder = mock(ServletUriComponentsBuilder.class);
}
@Test
...
You may even do this shorter
public class MockTest {
ServletUriComponentsBuilder servletUriComponentsBuilder
= mock(ServletUriComponentsBuilder.class);
@Test
...
Upvotes: 3