我想运行一个扩展ActivityInstrumentationTestCase2的测试类。但我得到了下一个日志。 我将断点设置为Oncreate的目标活动,但它甚至没有去那里,然后我得到了错误。
[2012-07-30 18:14:42 - myapp] Uploading myapp.apk onto device '66313d1c'
[2012-07-30 18:14:47 - myapp] Installing myapp.apk...
[2012-07-30 18:14:57 - myapp] Success!
[2012-07-30 18:14:57 - mytestapp] Launching instrumentation android.test.InstrumentationTestRunner on device 66313d1c
[2012-07-30 18:14:57 - mytestapp] Collecting test information
[2012-07-30 18:14:58 - mytestapp] Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'
我这样做了:
adb shell pm list instrumentation
我可以看到我的测试应用程序。
整个日志:
07-30 20:37:16.172: W/dalvikvm(19499): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
07-30 20:37:16.202: E/AndroidRuntime(19499): FATAL EXCEPTION: main
07-30 20:37:16.202: E/AndroidRuntime(19499): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{mytestapp/android.test.InstrumentationTestRunner}: java.lang.RuntimeException: Could not instantiate test class. Class:myapp.activity.myactivityTest
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3257)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.os.Looper.loop(Looper.java:130)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-30 20:37:16.202: E/AndroidRuntime(19499): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 20:37:16.202: E/AndroidRuntime(19499): at java.lang.reflect.Method.invoke(Method.java:507)
07-30 20:37:16.202: E/AndroidRuntime(19499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-30 20:37:16.202: E/AndroidRuntime(19499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-30 20:37:16.202: E/AndroidRuntime(19499): at dalvik.system.NativeStart.main(Native Method)
07-30 20:37:16.202: E/AndroidRuntime(19499): Caused by: java.lang.RuntimeException: Could not instantiate test class. Class:myapp.activity.myactivityTest
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.AndroidTestRunner.runFailed(AndroidTestRunner.java:233)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.AndroidTestRunner.buildSingleTestMethod(AndroidTestRunner.java:100)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:51)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:81)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:418)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:399)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:364)
07-30 20:37:16.202: E/AndroidRuntime(19499): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3254)
myactivityTestactivity
package myapp;
import myapp.R;
import android.test.ActivityInstrumentationTestCase2;
public class myactivityTest extends ActivityInstrumentationTestCase2<myactivity> {
public ImportFileSelectorTest(String name) {
super("myactivity", myactivity.class);
}
protected final void setUp() throws Exception {
super.setUp();
}
protected final void tearDown() throws Exception {
super.tearDown();
}
public void testSimpleDefaults(){
assertTrue(true);
}
}
任何提示都会有所帮助。谢谢。
答案 0 :(得分:0)
在我的情况下,类的构造函数抛出异常所以我在构造函数中处理了该异常并且它开始正常工作!但很奇怪,但这很有效:)
所以它就像public classname throws exception(){}
,我改为public classname (){try...catch}