RuntimeException的同时使用ActivityUnitTestCase,但不能同时ActivityInstrumentationTestCase2 [英] RuntimeException while using ActivityUnitTestCase, but not while ActivityInstrumentationTestCase2

查看:451
本文介绍了RuntimeException的同时使用ActivityUnitTestCase,但不能同时ActivityInstrumentationTestCase2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来测试ActivityUnitTestCase一个Android应用程序的MainActvity。出于某种原因,我甚至不能开始测试,因为它失败,出现以下错误跟踪:

I am trying to test the MainActvity of an Android application with ActivityUnitTestCase. For some reason, I cannot even start the test, because it fails with the following error trace:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1594)
Caused by: java.lang.NullPointerException: name == null
at java.lang.Class.getConstructorOrMethod(Class.java:446)
at java.lang.Class.getMethod(Class.java:915)
at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:253)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:189)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3943)
at android.app.ActivityThread.access$1300(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4447)
at java.lang.reflect.Method.invokeNative(Native Method)
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)

我无法调试它,因为它它得到任何我可以把测试用例类断点之前失败。任何测试我的类ActivityInstrumentationTestCase2工作做没有问题。

I cannot debug it, since it fails before it gets to any of the breakpoints I can put in the test case class. Any tests I am doing with the class ActivityInstrumentationTestCase2 work without problems.

在控制台中显示以下错误消息:

In the console the following error message is displayed:

Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'

我认为,这暗示getInstrumentation()不能成功调用。如果是这样,我怎么可能以其他方式开始在MainActivity创建一个有效的意图是什么?

I assume, that it hints that getInstrumentation() cannot be successfully called. If so, how could I otherwise create a valid Intent for starting the MainActivity?

在code。与ActivityUnitTestCase:

The code with ActivityUnitTestCase:

public class MainActivityUnitTest extends 
    android.test.ActivityUnitTestCase<MainActivity> {

    private MainActivity activity;

    public MainActivityUnitTest(String name) {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();  
    }

    @SmallTest
    public void testActivity() {

        Intent intent = new Intent(getInstrumentation().getTargetContext(),
                MainActivity.class);
            startActivity(intent, null, null);
            activity = getActivity();       
        assertNotNull(activity);
    }
}

感谢您提供任何帮助。

Thank you for any help provided.

推荐答案

的问题是在构造函数参数名。当删除它,它跑得很清楚。 :)

The problem was in the parameter "name" in the constructor. When removing it, it ran perfectly well. :)

这篇关于RuntimeException的同时使用ActivityUnitTestCase,但不能同时ActivityInstrumentationTestCase2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆