这是我的POJO类:
/**
* a POJO class to store json response data
*/
public class SearchItem {
private String headline="";
private String mDefinition="";
private String nivURL="";
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getImageURL() {
return nivURL;
}
public void setImageURL(String URL) {
this.nivURL = URL;
}
public String getDefinition() {
return mDefinition;
}
public void setDefinition(String definition) {
this.mDefinition = definition;
}
@Override
public String toString() {
return "[ headline=" + headline +
" , imageView URL=" + nivURL + "]";
}
}
这是我试图模拟班级的简单测试:
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;
import org.mockito.Mockito;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {
MainActivity mActivity;
private Button goBtn;
private RecyclerListAdapter mAdapter;
public ApplicationTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
mActivity = getActivity();
goBtn=(Button)mActivity.findViewById(
R.id.btn_go);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPreconditions() {
assertTrue(mActivity.isNetworkAvailable());
isLayoutValid();
}
public void isLayoutValid(){
assertNotNull(goBtn);
}
public void testSomething(){
//this fails
Mockito.mock(SearchItem.class);
}
}
下面是日志输出:
Running tests
Test running started
java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:96)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:60)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:52)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:32)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1258)
at org.mockito.Mockito.mock(Mockito.java:1135)
at mypackage.ApplicationTest.testSomething(ApplicationTest.java:65)
这是我的Android Studio Gradle依赖项:
dependencies {
compile files('libs/volley.jar')
apt "org.androidannotations:androidannotations:3.0+"
compile "org.androidannotations:androidannotations-api:3.0+"
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.jakewharton:butterknife:6.0.0'
compile 'com.squareup.dagger:dagger:1.2.2'
compile 'com.squareup:javawriter:2.5.1'
compile 'javax.inject:javax.inject:1'
compile 'org.mockito:mockito-all:1.10.8'
apt 'com.squareup.dagger:dagger-compiler:1.2.2'
}
如何解决模拟错误?
尝试将 dexmaker 库添加到您的依赖项中。
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0',
androidTestCompile 'com.google.dexmaker:dexmaker:1.0',
您需要包含这些内容,以便 mockito 可以与您的 dex 文件正确交互。
更好的解释可以在这里找到:http://corner.squareup.com/2012/10/mockito-android.html