gpt4 book ai didi

Android JUnit 测试 ClassNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:06 26 4
gpt4 key购买 nike

我正在尝试设置 JUnit 来测试我在 Android 项目中的 Activity 。

Android 项目在 AVD 和设备上都运行良好(仍然存在一些错误,这就是为什么我想添加一些单元测试)

我遵循了这些网站的步骤(它们或多或少是一样的,但我仍然检查了所有这些网站以确保我做的一切都是正确的)
http://mobile.tutsplus.com/tutorials/android/android-sdk-junit-testing/
http://developer.android.com/tools/testing/testing_eclipse.html

我使用与我的项目相同的工作区创建了一个测试项目,并创建了我的第一个测试用例。当我尝试运行测试时,我得到以下信息:

[2012-12-09 19:42:56 - AssassinTest] Android Launch!
[2012-12-09 19:42:56 - AssassinTest] adb is running normally.
[2012-12-09 19:42:56 - AssassinTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2012-12-09 19:42:56 - AssassinTest] Automatic Target Mode: Preferred AVD 'Google_Level10' is available on emulator 'emulator-5554'
[2012-12-09 19:42:56 - AssassinTest] Uploading AssassinTest.apk onto device 'emulator-5554'
[2012-12-09 19:42:57 - AssassinTest] Installing AssassinTest.apk...
[2012-12-09 19:42:59 - AssassinTest] Success!
[2012-12-09 19:42:59 - AssassinTest] Project dependency found, installing: Assassin
[2012-12-09 19:43:01 - Assassin] Application already deployed. No need to reinstall.
[2012-12-09 19:43:01 - AssassinTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2012-12-09 19:43:01 - AssassinTest] Collecting test information
[2012-12-09 19:43:04 - AssassinTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

这是我从 LogCat 得到的:

12-09 19:43:02.929: E/dalvikvm(1255): Unable to resolve Ludes/assassi/test/InGameActivityTest; annotation class 19
12-09 19:43:02.929: D/AndroidRuntime(1255): Shutting down VM
12-09 19:43:02.929: W/dalvikvm(1255): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-09 19:43:02.961: E/AndroidRuntime(1255): FATAL EXCEPTION: main
12-09 19:43:02.961: E/AndroidRuntime(1255): java.lang.NoClassDefFoundError: org.junit.Test
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.Method.getDeclaredAnnotations(Native Method)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:262)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:188)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.AccessibleObject.getAnnotation(AccessibleObject.java:196)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
12-09 19:43:02.961: E/AndroidRuntime(1255): at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
12-09 19:43:02.961: E/AndroidRuntime(1255): at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:254)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3246)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.os.Looper.loop(Looper.java:130)
12-09 19:43:02.961: E/AndroidRuntime(1255): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.reflect.Method.invoke(Method.java:507)
12-09 19:43:02.961: E/AndroidRuntime(1255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-09 19:43:02.961: E/AndroidRuntime(1255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-09 19:43:02.961: E/AndroidRuntime(1255): at dalvik.system.NativeStart.main(Native Method)
12-09 19:43:02.961: E/AndroidRuntime(1255): Caused by: java.lang.ClassNotFoundException: org.junit.Test in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/system/framework/android.test.runner.jar:/data/app/udes.assassin.test-1.apk:/data/app/udes.assassin-2.apk]
12-09 19:43:02.961: E/AndroidRuntime(1255): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-09 19:43:02.961: E/AndroidRuntime(1255): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-09 19:43:02.961: E/AndroidRuntime(1255): ... 25 more

在几个论坛上搜索时,我读到这可能是由错误的 list 文件引起的,但我不明白我的问题是什么

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="udes.assassin.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="udes.assassin" />

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

最后,这是我唯一的 JUnit 类中的代码(到目前为止)

package udes.assassin.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import udes.assassin.InGameActivity;
import android.test.ActivityInstrumentationTestCase2;

public class InGameActivityTest
extends ActivityInstrumentationTestCase2<InGameActivity> {

public InGameActivityTest() {
super("udes.assassin", InGameActivity.class);
}

public InGameActivityTest(Class<InGameActivity> activityClass) {
super(activityClass);
}

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

@Test
public void testSetItemPosition() {
fail("Not yet implemented");
}
}

谢谢

最佳答案

这可能是因为 JUnit 不在您的类路径中。转到 Project -> Properties -> Java Build Path -> Order and Export 并检查 JUnit 框是否未选中。

关于Android JUnit 测试 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com