Reputation: 176
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)
Upvotes: 1
Views: 8433
Reputation: 176
I solved the problem by replacing mockito-core.jar
with mockito-all.jar
Upvotes: 5
Reputation: 1812
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.
Upvotes: 2