我有.apk文件,我想使用robotiun测试它,我有followed程序但是当我使用junit测试运行应用程序时,我收到错误测试运行失败:仪表运行因“流程崩溃”而失败。
这是我的androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.metago.astro.test" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.metago.astro" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
</manifest>
我的TestActivity.java文件是
@SuppressWarnings("unchecked")
public class TestActivity extends ActivityInstrumentationTestCase2 {
private static final String TARGET_PACKAGE_ID = "com.metago.astro";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.metago.astro.SplashActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public TestActivity() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME));
}
private Solo solo;
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testCanOpenSettings() {
solo.pressMenuItem(0);
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}
我的logcat是
FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.metago.astro.SplashActivity
at com.metago.astro.TestActivity.<clinit>(TestActivity.java:19)
... 15 more
Caused by: java.lang.ClassNotFoundException: com.metago.astro.SplashActivity
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:217)
at java.lang.Class.forName(Class.java:172)
at com.metago.astro.TestActivity.<clinit>(TestActivity.java:17)
... 15 more
Caused by: java.lang.NoClassDefFoundError: com/metago/astro/SplashActivity
Caused by: java.lang.ClassNotFoundException: com.metago.astro.SplashActivity
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
... 19 more
第17行是.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
答案 0 :(得分:1)
测试项目的程序包名称需要与要测试的应用程序的程序包名称不同。包名称必须是唯一标识符。在您的测试应用程序中,您应该使用类似package="com.metago.astro.test"
此外,您可能需要添加以下权限:
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />