我有以下课程:
public class SomeClass {
private int digit;
public SomeClass(int i){
digit = i;
}
public int someMethod(int i){
/*
* Some work
*/
}
/**
* Other method
*/
}
当我想用Mockito创建这个类的间谍时,我得到java.lang.NoClassDefFoundError
但是当我将方法改为
时public int someMethod(){
// some work
}
所有工作都没有错误。我做错了什么?
我的测试班:
@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
*/
}
}
编辑堆栈跟踪,异常
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)
答案 0 :(得分:5)
我通过将mockito-core.jar
替换为mockito-all.jar
答案 1 :(得分:2)
我不认为它与模拟有任何关系。 NoClassDefFoundError错误即将发生,因为您的SomeClass.class在执行期间不在类路径中。把它放在类路径上,错误就会消失。