I've been facing a peculiar problem. Basically, when I run my Mockito test normally i.e. 'Run as Junit Test', it gives me the following error. Can someone help me please what is my mistake?
java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;
at org.powermock.api.mockito.mockmaker.MockMakerLoader.doLoad(MockMakerLoader.java:45)
at org.powermock.api.mockito.mockmaker.MockMakerLoader.load(MockMakerLoader.java:36)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:36)
... shortened stacktrace....
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
public class ListTest {
@Test
public void letsMockListSize() {
List<?> list= mock(List.class);
when(list.size()).thenReturn(2);
assertEquals(2, list.size());
}
}
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
Try Mockito 2.10.0 and Powermock 2.0.9. I also had to exclude net.bytebuddy:byte-buddy from my springfox-core import. I was getting a similar issue, when trying to use Mockito with Junit 5 (@ExtendWith)
As a solution you can downgrade the Mockito version or you can use the newer version of Mockito to mock your final classes or methods. There is a ticket opened regarding this issue here.
If you really want to use 1.10.19, you should probably use the then correct class org.mockito.runners.MockitoJUnitRunner which then should work. But I would strongly recommend using a newer Mockito version instead. mockito-all is simply not the right artifact to depend on anymore.
you could use annotation @ExtendWith (MockitoExtension.class) in your test call. in the class of test use @InjectMocks to inject the Service you need to test, this will allow @Mock for his dependancies call.
The sample works if you remove the PowerMock dependencies. The issue is that the versions of Mockito and PowerMock used are incompatible with each other. For PowerMock 2.x you need at least Mockito 2.8.9+. PowerMock provides a compatibility list that shows what version of PowerMock is compatible with what version of Mockito. Fix up the versions to be compatible with each other and your sample will start to work.
I was getting a similar issue, when trying to use Mockito with Junit 5 (@ExtendWith)
java.lang.NoSuchMethodError: org.mockito.Mockito.mockitoSession()Lorg/mockito/session/MockitoSessionBuilder;
This was coming as there were two versions of mockito present in the classpath (one is mockito-all and another is mockito-core), through transitive dependency. After removing the mockito-all dependency from POM explicitly, the issue got resolved.
@ExtendWith(MockitoExtension.class)
public class TodoBusinessImplWIthMockWithAnnotationTest {
@Mock
TodoService todoService;
@Test
public void usingMockito() {
......
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With