Java – runtimeException when using activityunittestcase, but not runtimeException when using activityinstrumentationtestcase2

I'm trying to test the mainactivity of an Android application using activityunittestcase. For some reason, I can't 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)

I can't debug it because it fails before it reaches any breakpoint I can put in the test case class. Any tests I'm doing with the activityinstrumentationtestcase2 class are fine

The following error message is displayed in the console:

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

I assume that it implies that getinstrumentation () cannot be called successfully. If so, how can I create a valid intent to start mainactivity?

Code of 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

resolvent:

The problem is in the parameter "name" in the constructor. It works very well when it is removed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>