在 Netbeans 中运行 JUnit 测试时出现 java.lang.NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:24:45  来源:igfitidea点击:

java.lang.NoClassDefFoundError while running JUnit test in Netbeans

javaandroidjunit

提问by Chandru K

I am building an Android hello world application in Netbeans. It's building properly and I am able to run in the emulator also.

我正在 Netbeans 中构建一个 Android hello world 应用程序。它正在正确构建,我也可以在模拟器中运行。

But when creating and running the Junit test I get a java.lang.NoClassDefFoundError.

但是在创建和运行 Junit 测试时,我得到了一个java.lang.NoClassDefFoundError.

How can I fix this problem?

我该如何解决这个问题?

回答by philippe_b

Check the manifest of your test project, for example HelloWorldTest/AndroidManifest.xml. The android:targetPackageattribute of the instrumentationmarkup should target the package in your application, for example, com.example.helloworld. Notthe test package of your test project (eg. com.example.helloworld.test).

检查您的测试项目的清单,例如HelloWorldTest/AndroidManifest.xml。标记的android:targetPackage属性instrumentation应针对应用程序中的包,例如com.example.helloworld. 不是您的测试项目的测试包(例如com.example.helloworld.test)。

回答by Eng.Fouad

Simply AndroidManifest.xml -> manifest -> packageof main application should match AndroidManifest.xml -> manifest -> instrumentation -> android:targetPackageof the test application.

简单AndroidManifest.xml -> manifest -> package的主应用程序应该匹配AndroidManifest.xml -> manifest -> instrumentation -> android:targetPackage测试应用程序。

回答by Andreas Dolk

The full error message contains the name of the class, that wasn't found on the classpath. Double check if the classpath that is used for running the test includes all required classes and libraries (your class files, junit.jar, android specific libaries).

完整的错误消息包含在 classpath 中找不到的类的名称。仔细检查用于运行测试的类路径是否包括所有必需的类和库(您的类文件、junit.jar、android 特定库)。

回答by gpampara

I find that a rebuild usually finds the classes (as it is a classpath issue). Netbeans seems to aggressively compile existing tests, but the main source code (from a maven setting at least) is not always available.

我发现重建通常会找到类(因为它是类路径问题)。Netbeans 似乎积极编译现有测试,但主要源代码(至少来自 maven 设置)并不总是可用。

I've started running full builds to try address this. There might be a plugin that addresses this but I haven't found it yet.

我已经开始运行完整版本来尝试解决这个问题。可能有一个插件可以解决这个问题,但我还没有找到。

EDIT: Thismight help.

编辑:可能会有所帮助。

回答by Draiken

I don't know about netbeans specifics, but the problem is probably the same.

我不知道 netbeans 的具体情况,但问题可能是一样的。

FragmentActivity can not be tested via ActivityInstrumentationTestCase2

FragmentActivity 无法通过 ActivityInstrumentationTestCase2 进行测试

Your test project can be using a different android compatibility library than your main project, and that causes this weird errors.

您的测试项目可能使用与您的主项目不同的 android 兼容性库,这会导致这种奇怪的错误。