1

I have the following class:

public class SomeClass {

    private int digit;

    public SomeClass(int i){
        digit = i;
    }       
    public int someMethod(int i){
        /*
         * Some work
         */
    }    
    /**
     * Other method
     */
}

And when I want to create a spy of this class with Mockito, I get java.lang.NoClassDefFoundError

But when I change method to

public int someMethod(){
    // some work
}

all works without an error. What am I doing wrong?
My test class:

@PrepareForTest ({SomeClass.class})
public class SomeClassTest extends AndroidTestCase {

    private SomeClass someClass = null;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // This is necessary on devices in order to avoid bugs with instantiation order
        System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());

        SomeClass localSomeClass = new SomeClass(10);
        someClass = Mockito.spy(localSomeClass);
        Mockito.doReturn(5).when(someClass).someMethod();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testCorrectExemption(){
        /**
         * Test code
         */
    }
}

Edit: stacktrace with exception

java.lang.NoClassDefFoundError: org.mockito.internal.matchers.Equals
    at org.mockito.internal.invocation.ArgumentsProcessor.argumentsToMatchers(ArgumentsProcessor.java:47)
    at org.mockito.internal.invocation.InvocationMatcher.<init>(InvocationMatcher.java:34)
    at org.mockito.internal.invocation.MatchersBinder.bindMatchers(MatchersBinder.java:26)
    at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:50)
    at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
    at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
    at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49)
    at SomeClass_Proxy.testMethod(SomeClass_Proxy.generated)
    at ru.test.SomeClassTest.setUp(SomeClassTest.java:47)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
3
  • At what line is the exception thrown? Mar 27, 2015 at 15:22
  • Please post the stacktrace.
    – Ria
    Mar 27, 2015 at 17:05
  • Exception throws on Mockito.doReturn(5).when(someClass).someMethod(); stacktrace will later
    – temq
    Mar 28, 2015 at 11:21

2 Answers 2

5

I solved the problem by replacing mockito-core.jar with mockito-all.jar

2

I don't think it has anything to do with mock. NoClassDefFoundError error is coming because your SomeClass.class is not in class path during execution. Put it class path and error will go away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.