java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd with Mockito

ClassNotFoundException is result of a class loader that is not able to load a particular class.

In your case Mockito has a transitive dependency to Objenesis (it needs Objenesis for correct behavior). You are most likely trying to execute your test with Mockito on test class path, but without Objenesis.

You need to add Objenesis to your test class path.

For maven projects, be sure that:

  1. you have declared Mockito as test dependency

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    
  2. to run a particular test from the command line execute

    mvn test -Dtest=fullyQualifedNameToYourTestClass
    

You can try adding the mockito-all artifact instead of mockito-core, it works since version 1.9.5


I was getting the same error of:

java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd

when I was running a test in a new project that was using Mockito.

Turns out in addition to adding the Mockito Dependencies I also had to add the Objenesis dependency. All I need to do was add the below dependency to my pom.xml and it all worked perfectly fine.

<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>2.3</version>
    <scope>test</scope>
</dependency>