Reputation: 4232
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>
Upvotes: 18
Views: 38460
Reputation: 111
remove to your pom.xml mockito-all and use mockito-core
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
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.
Upvotes: 3
Reputation: 71
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() {
......
}
}
Upvotes: 4
Reputation: 39
If you are using a newer version of SpringBoot it may came with a version of Mockito bigger than 3.12.4 and this may make your powermock-api-mockito2 not work because in the newer versions of Mockito the get() method from org.mockito.Answers was deleted, it was already deprecated in 3.12.4, and the powermock-api-mockito was not adapted to work with the newer version.
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.
Upvotes: -1
Reputation: 3549
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.
Upvotes: 14