3

我有一个 Android 应用程序,我正在尝试在 Eclipse 中使用 JUnit 进行测试。我创建了一个 Android 测试项目并添加了一些测试,但是当我尝试在我的设备上运行测试时,每个测试都失败,并在我尝试测试的类上出现 java.lang.NoClassDefFoundError。

我发现了一些类似的问题,但与我的问题完全不同。最接近的是this,但是这个用户正在使用 Maven 并想出了一个解决方法。我没有使用 Maven。

以下是 LogCat 的一些输出:

03-26 13:36:05.281: I/TestRunner(8380): started: testValueOf(com.evertz.android.vlpro.unit.SeverityTest)
03-26 13:36:05.281: I/TestRunner(8380): failed: testValueOf(com.evertz.android.vlpro.unit.SeverityTest)
03-26 13:36:05.281: I/TestRunner(8380): ----- begin exception -----
03-26 13:36:05.281: I/TestRunner(8380): java.lang.NoClassDefFoundError: com.evertz.android.vlpro.Severity
03-26 13:36:05.281: I/TestRunner(8380):     at com.evertz.android.vlpro.unit.SeverityTest.testValueOf(SeverityTest.java:19)
03-26 13:36:05.281: I/TestRunner(8380):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 13:36:05.281: I/TestRunner(8380):     at java.lang.reflect.Method.invoke(Method.java:511)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestCase.runTest(TestCase.java:168)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestCase.runBare(TestCase.java:134)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestResult$1.protect(TestResult.java:115)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestResult.runProtected(TestResult.java:133)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestResult.run(TestResult.java:118)
03-26 13:36:05.281: I/TestRunner(8380):     at junit.framework.TestCase.run(TestCase.java:124)
03-26 13:36:05.281: I/TestRunner(8380):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
03-26 13:36:05.281: I/TestRunner(8380):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
03-26 13:36:05.281: I/TestRunner(8380):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
03-26 13:36:05.281: I/TestRunner(8380):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
03-26 13:36:05.281: I/TestRunner(8380): ----- end exception -----
03-26 13:36:05.281: I/TestRunner(8380): finished: testValueOf(com.evertz.android.vlpro.unit.SeverityTest)

我还有其他带有 JUnit 测试的 Android 项目,它们工作得很好。这是一个简短的总结:

  • Utils:一个 Android 库项目,其中包含从我现有的 Java 代码库复制的多个实用程序类并经过 Android 化。
  • UtilsTest:Utils项目中类的对应单元测试。有对 easymock.jar 的引用。这些测试在设备上运行得非常好。
  • AppOne:一个简单的 Android 应用程序,不依赖于 Utils、EasyMock 或其他任何东西。
  • AppOneTest:AppOne 类的单元测试。这些测试也运行良好。
  • AppTwo:一个依赖于 Utils 库项目的 Android 应用程序。
  • AppTwoTest:AppTwo 类的单元测试。依赖于 Utils 和 EasyMock。如上所述,所有这些测试都失败了(找不到被测类)。

所有测试类都使用 JUnit3 语义(扩展 TestCase、无注释等)。测试中的所有类都不是基于活动的(还)。我现在只是在测试支持类,这些只对 Android SDK 类(例如 android.util.Log、android.graphics.Color)有少量引用。

我认为具有外部依赖关系可能会以某种方式导致问题,所以我尝试在 AppOne 和 AppOneTest 中添加一些(不必要的)对 Utils 类的调用,但这些测试仍然有效。我还尝试从 AppTwoTest 中删除所有测试,除了不引用 Utils 或 EasyMock(LogCat 输出中的 SeverityTest 类)的测试,但仍然失败。

我尝试了干净的构建,重新启动 Eclipse,删除并重新创建 AppTwoTest 项目,但似乎没有任何效果。关于可能导致此问题的任何想法?

4

1 回答 1

1

检查测试应用的清单,它应该包含以下内容:

  1. 测试代码的包定义

  2. Uses-sdk 适合您的应用

  3. 在测试运行器中链接的使用库

  4. 带有指向应用程序代码的链接的检测

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"      <!-- package of the test code -->
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.app" />      <!-- package of the app code -->
    
    <application android:allowBackup="false">
        <!-- linkage for the test runner -->
        <uses-library android:name="android.test.runner" />     
    </application>
    

于 2013-03-27T02:01:04.187 回答