我尝试安装Mockito并收到该错误消息。当我得到它时,我会回来添加我自己的答案。
它发生在真正的 MainActivity 及其衍生的 MockActivity 测试端。
它也发生在一个新的Android项目中,Android版本4.3和最近的Eclipse。加上模拟 1.9.5。有什么问题吗,维护者?
测试用例很简单,并且使用ActivityUnitTestCase<MainActivity>
.
以前有人见过这种堆栈跟踪吗?
org.mockito.exceptions.base.MockitoException: Failed to mock class com.example.example.test.MockActivity
at com.example.example.test.SmartPhinsTest.test_mockito_mocks_activities(ExampleTest.java:39)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
有人可能已经找到了测试框架标签吗?
(微不足道的)代码截图是:
package com.example.example.test;
import java.io.File;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import com.example.example.MainActivity;
import static org.mockito.Mockito.*;
public class ActivityTests extends ActivityUnitTestCase<MainActivity> {
public ActivityTests(Class<MainActivity> activityClass) {
super(activityClass);
}
public ActivityTests() {
super(MainActivity.class);
}
Activity activity;
Context targetContext;
Intent intent;
@Override
protected void setUp() throws Exception {
targetContext = getInstrumentation().getTargetContext();
intent = new Intent(targetContext, MainActivity.class);
super.setUp();
startActivity(intent, null, null);
activity = getActivity();
}
public void test_mockito_mocks_activities() {
Context context = activity.getApplicationContext();
File cacheDir = context.getCacheDir();
String path = cacheDir.getPath();
System.setProperty("dexmaker.dexcache", path); // "/storage/sdpath");
MainActivity mockActivity = mock(MainActivity.class);
}
}
我还向两个AndroidManifest.xml
文件添加了android.permission.WRITE_EXTERNAL_STORAGE
。而且,尽管大多数示例都使用 ActivityInstrumentationTestCase2
,它拒绝为我工作 - 即使在另一个平台上的绿地项目中也是如此。
这相当一致地给了我:
E/dalvikvm(3576): Could not find class 'android.app.LoaderManagerImpl', referenced from method MainActivity_Proxy.getLoaderManager
E/dalvikvm(3576): Could not find class 'android.app.Activity$NonConfigurationInstances', referenced from method MainActivity_Proxy.retainNonConfigurationInstances
在设置方法中输入:
// To solve some issue with Dexmaker
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());