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

java lang noclassdeffounderror org/mockito session mockitosessionlogger
java.lang.noclassdeffounderror: org/mockito/mockitoannotations$mock
java lang noclassdeffounderror org/mockito/cglib/proxy/enhancer
java lang noclassdeffounderror org/mockito/cglib/proxy/methodinterceptor
java lang noclassdeffounderror org/mockito/internal/mockitoinvocationhandler
java lang noclassdeffounderror org/mockito/internal/creation/cglibmockmaker
java lang noclassdeffounderror libcore util emptyarray

I don't know why I have that error with mockito

java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd
at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(
at org.mockito.internal.creation.jmock.ClassImposterizer.<clinit>(
at org.mockito.internal.util.MockCreationValidator.isTypeMockable(
at org.mockito.internal.util.MockCreationValidator.validateType(
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(
at org.mockito.internal.creation.MockSettingsImpl.confirm(
at org.mockito.internal.MockitoCore.mock(
at org.mockito.Mockito.mock(
at org.mockito.Mockito.mock(
at fr.oap.SubscriptionTest.testGetSubscriptionById(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at junit.framework.TestCase.runTest(
at junit.framework.TestCase.runBare(
at junit.framework.TestResult$1.protect(
at junit.framework.TestResult.runProtected(
at junit.framework.TestSuite.runTest(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
Caused by: java.lang.ClassNotFoundException: org.objenesis.ObjenesisStd
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
... 29 more

About my class of Test is like this :

import junit.framework.TestCase;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import fr.aop.subscription.AbstractSubscription;
public class SubscriptionTest extends TestCase {
public void testGetSubscriptionById() {
     Subscription objMock=Mockito.mock(Subscription.class);        

And about the method getSubscribById whitch is in the class Subscription:

public AbstractSubscription getSubscriptionById(final Integer id) {
    this.log.debug("BEGIN: getSubscriptionById id = " + id);
    AbstractSubscription obj = null;
    if (id != null) {
        final StringBuilder queryString = new StringBuilder("select c from AbstractSubscription c ");

        try {
            queryString.append("where = :id");
            Query query = this.getEntityManager().createQuery(queryString.toString());
            query = query.setParameter("id", id);
            obj = (AbstractSubscription) query.getSingleResult();
        } catch (final Exception exc) {

    return obj;

when I instanciate the Subcription class it demand me the connection to the database, that's why I want to escape this and looking for a solution like 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

  2. to run a particular test from the command line execute

    mvn test -Dtest=fullyQualifedNameToYourTestClass

41. 42. 43. java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd at org.mockito.internal.util. Mockito.mock( 1243 ). I'm also including the dexmaker-mockito since the app is multi-dex. I suspect it's something to do with that, but I'm at a loss. I'm hoping you guys have seen this before and have a workaround, this way if somebody else runs into this they don't have to spend an entire day trying to resolve it.

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

Hello, When I tried to run mockito tests I got a java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd. I tried to fix this by adding the  I don't khnow why i have that error with mockito java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd at org.mockito.internal.creati

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.


I don't khnow why i have that error with mockito. java.lang.​NoClassDefFoundError: org/objenesis/ObjenesisStd mockito-all-1.9.5 mockito-core-1.10.19 I imported the project via import->existing Maven project (by including the pom.xml) where in Idea I am doing similar things as well

I had a similar problem in an Android project using gradle.

Like @Popeye did for maven, I added the following line to build.gradle, among dependencies:

testImplementation 'org.objenesis:objenesis:2.3'

That solved my problem.

This page provides Java code examples for org.objenesis.ObjenesisStd. addClass(AbstractServerTest.class); // add Mockito testJar.addPackages(true  Dans votre cas, Mockito a une dépendance transitive de Objenesis (il doit Objenesis pour corriger le comportement). Vous êtes le plus susceptible d'essayer de l'exécuter vos tests avec Mockito sur test de chemin de classe, mais sans Objenesis.

I have this issue with the version of mockito-core 2.22.0.

After investigation it seems that the jar of objenesis was corrupted in my maven repository...

Just a rm -rf .m2/repository/org/objenesis is enough

Eu não sei porque eu tenho esse erro com mockito Java.lang.​NoClassDefFoundError: org/objenesis/ObjenesisStd at org.mockito.internal.​creation.jmock. Now, I tried to figure out which JAR contains org.springframework.objenesis.ObjenesisStd by using, but it can't return any Spring related JARs. What am I missing here? spring spring-data-mongodb objenesis

ClassNotFoundException est le résultat d'un chargeur de classe qui n'est pas en mesure de charger une classe particulière. Dans votre cas Mockito a une  Objenesis is a small Java library that serves one purpose: To instantiate a new object of a particular class. When would you want this? Java already supports this dynamic instantiation of classes using Class.newInstance(). However, this only works if the class has an appropriate constructor.

java.lang.NoClassDefFoundError: org / objenesis / ObjenesisStd с Mockito 2015-01-19 unit-testing testing junit mocking mockito Я не знаю, почему у меня такая ошибка с mockito

  • just add objenesis in your classpath
  • And how it's possible to do that
  • How are you running your tests? Using Maven, Ant, from command line?
  • i have a maven project but i make a right clic on my classtest and i choose run as JunitTest
  • I updated the comment. You should have the Mockito test dependency in your POM and not excluded the Objenesis. If that does not work, it is probably an IDE bug. It should be always working from the command line.
  • Cool, can you mark the answer as accepted and close this question?
  • mockito-all is deprecated: Mockito does not produce the mockito-all artifact anymore ; this one was primarily aimed at ant users, and contained other dependencies. We felt it was time to move on and remove such artifacts as they cause problems in dependency management system like maven or gradle.