Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework

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?

The error received:

    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)

The test class:

public class ListTest {
    @Test
    public void letsMockListSize() {
        List<?> list= mock(List.class);
        when(list.size()).thenReturn(2);
        assertEquals(2, list.size());
    }
}

pom.xml

<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>
like image 446
Krish Avatar asked Aug 18 '19 06:08

Krish


People also ask

What version of Mockito is used with JUnit 5?

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)

Is it possible to downgrade Mockito 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.

Should I use Mockito-all or mockitojunitrunner?

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.

How to use @mockitoextension in Test call?

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.


2 Answers

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.

like image 55
Maik Avatar answered Sep 19 '22 20:09

Maik


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() {

    ......

   }

}
like image 26
Sushanta Sinha Avatar answered Sep 20 '22 20:09

Sushanta Sinha