2012-07-06 95 views
2

我一直在與Android單元測試一起插入,並遇到了似乎是路徑問題。Android單元測試包路徑錯誤

首先我在我的項目目錄

android create test-project -m ../ -p tests -n RoamPayTests 

這將創建一個測試項目,並初始化它內運行從我的這個命令。自動生成的ActivityInstrumentaionTestCase2工作正常。 我可以在這裏創建測試,一切正常。

注意:請注意,主要活動在ui包中。

package com.roamdata.roampayx; 

import com.roamdata.roampayx.ui.SplashActivity; 

import android.test.ActivityInstrumentationTestCase2; 

/** 
* This is a simple framework for a test of an Application. See 
* {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on 
* how to write and extend Application tests. 
* <p/> 
* To run this test, you can type: 
* adb shell am instrument -w \ 
* -e class com.roamdata.roampayx.ui.SplashActivityTest \ 
* com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner 
*/ 
public class SplashActivityTest extends ActivityInstrumentationTestCase2<SplashActivity> { 

    public SplashActivityTest() { 
     super("com.roamdata.roampayx", SplashActivity.class); 
    } 

這裏產生Android_Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us --> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.roamdata.roampayx.tests" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <!-- We add an application tag here just so that we can indicate that 
     this package needs to link against the android.test library, 
     which is needed when building test cases. --> 
    <application> 
     <uses-library android:name="android.test.runner" /> 
    </application> 
    <!-- 
    This declares that this application uses the instrumentation test runner targeting 
    the package of com.roamdata.roampayx. To run the tests use the command: 
    "adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner" 
    --> 
    <instrumentation android:name="android.test.InstrumentationTestRunner" 
        android:targetPackage="com.roamdata.roampayx" 
        android:label="Tests for com.roamdata.roampayx"/> 
</manifest> 

汽車的問題是,當我嘗試創建一個新的測試用例。

package com.roamdata.roampayx; 
import com.roamdata.roampayx.provider.RoamPay; 
import com.roamdata.roampayx.provider.RoamPayProvider; 
import android.test.ProviderTestCase2; 


public class ProviderTest extends ProviderTestCase2<RoamPayProvider> { 
    public ProviderTest() { 
     super(RoamPayProvider.class, RoamPay.AUTHORITY); 
    } 
} 

接下來我建立並安裝,(這裏沒有問題)。但後來我跑

adb shell am instrument -w com.roamdata.roampayx.tests/android.test.InstrumentationTestRunner 

,最後我得到以下錯誤

android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests: 
Error in testSuiteConstructionFailed: 
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:190) 
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) 
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) 
Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Constructor.constructNative(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
    at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87) 
    at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73) 
    at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262) 
    at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184) 
    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4142) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:130) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NoClassDefFoundError: com.roamdata.roampayx.provider.RoamPayProvider 
    at ProviderTest.<init>(ProviderTest.java:9) 
    ... 18 more 

我有進口RoamPay和RoamPayProvider。 智能感知正在工作,我得到這些參考。

任何想法?

回答

1

除構造函數外,一切都很好。我改成

public RoamPayProviderTest() { 
    super(RoamPayProvider.class, "com.roamdata.roampayx"); 
} 

現在一切正常。我想你可以使用這個問題和答案作爲一個很好的教程,如何設置你的單元測試。

+0

爲什麼在這裏包含必需的包名? – 2014-06-23 22:08:46