无法将测试用例添加到TestSuite

时间:2013-04-16 12:34:31

标签: android junit

我编写了Runner类来执行测试套件,但我经常得到java.lang.RuntimeException:套件构造期间出现异常。方法名称不能为空。

import junit.framework.TestSuite;
import android.test.InstrumentationTestRunner;
import android.test.InstrumentationTestSuite;

public class Runner extends InstrumentationTestRunner {


public Runner() {
    super();
}



@Override
public TestSuite getAllTests() {
    InstrumentationTestSuite suite = new InstrumentationTestSuite(this);
    suite.addTest(new AndroidTest());
    return suite;
}

@Override
public ClassLoader getLoader() {
    return Runner.class.getClassLoader();
}

}

我的测试用例文件。

import junit.framework.Assert;
import android.test.ActivityTestCase;

public class AndroidTest extends AndroidTestCase{

     @Override
        protected void setUp() throws Exception {
            // TODO Auto-generated method stub
            super.setUp();
        }

     String nuber;
     public AndroidTest(String number1) {
         System.out.println("paramertized");
         this.nuber=number1;

    }
     public AndroidTest() {
         super();
         System.out.println("default");
    }




    public void testSomething() throws Throwable {
        System.out.println(nuber);
           Assert.assertTrue(1 + 1 == 2);
        }

        public void testSomethingElse() throws Throwable {
           Assert.assertTrue(1 + 1 == 3);
        }

}

我的清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.co.rakuten.sdt.sampleapp.test"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
    <uses-sdk android:minSdkVersion="9" />

    <instrumentation
        android:name="com.sampleapp.test.Runner"
        android:targetPackage="com.sampleapp" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

我得到的例外。

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
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:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Caused by: java.lang.NullPointerException: Method name must not be null.
at java.lang.ClassCache.findMethodByName(ClassCache.java:238)
at java.lang.Class.getMethod(Class.java:962)
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:254)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:190)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3246)
at android.app.ActivityThread.access$2200(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

好的,我可以重现你的问题。

通过删除public AndroidTest(String)构造函数并将值设置为String变量nuber,我可以运行测试。

String nuber ="";
/*
public AndroidTest(String number1) {
    super();
    System.out.println("paramertized");
    this.nuber=number1;

}
*/
public AndroidTest() {
    super();
    System.out.println("default");
}

A similar question posted here on SO