无法在 Eclipse Android 项目中运行 JUnit 4 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2172152/
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
Can't run JUnit 4 test case in Eclipse Android project
提问by Roaders
I am new to Java and am trying to run a unit test on a class I am writing. Eclipse (3.5) created the unit test class for me and added Junit4 to my class path.
我是 Java 新手,正在尝试对我正在编写的类运行单元测试。Eclipse (3.5) 为我创建了单元测试类并将 Junit4 添加到我的类路径中。
My Class:
我的课:
public class DistanceUtil
{
public static double metersToMiles( double meters )
{
return 0;
}
public static double metersToKilometers( double meters )
{
return 0;
}
}
My unit test:
我的单元测试:
public class DistanceUtilTest {
@Test
public final void testMetersToMiles() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testMetersToKilometers() {
fail("Not yet implemented"); // TODO
}
}
When I right click on the unit test and select run as Junit Test I get the following:
当我右键单击单元测试并选择作为 Junit 测试运行时,我得到以下信息:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:3075), pid=5564, tid=4940
# Error: ShouldNotReachHere()
#
# JRE version: 6.0_17-b04
# Java VM: Java HotSpot(TM) Client VM (14.3-b01 mixed mode windows-x86 )
# An error report file with more information is saved as:
# C:\Users\Giles Roadnight\workspaceAndroid\Cycloid\hs_err_pid5564.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Anyone got an idea how I can fix this?
任何人都知道我该如何解决这个问题?
Thanks
谢谢
回答by Alex Spurling
Having searched Google for an answer, this looks like it might have something to do with Android development tools.
在 Google 上搜索了答案后,这看起来可能与 Android 开发工具有关。
Below are steps taken from this commentthread:
以下是从该评论线程中采取的步骤:
- Right click on the project -> run -> run configuration
- Select your Junit project
- Go to the classpath tab
- remove the Android framework entry
- select bootstrap entries
- click on advanced
- select Add Library
- Ok
- Chose "JRE System Library"
- Next
- finish
- You need to also add the JUnit library so follow the steps 5 to 11 and select the "Junit" instead of "JRE System Library"
- You can now run your project as Junit.
- 右键项目->运行->运行配置
- 选择您的 Junit 项目
- 转到类路径选项卡
- 删除Android框架条目
- 选择引导条目
- 点击高级
- 选择添加库
- 好的
- 选择“JRE 系统库”
- 下一个
- 结束
- 您还需要添加 JUnit 库,因此请按照步骤 5 到 11 操作并选择“Junit”而不是“JRE 系统库”
- 您现在可以作为 Junit 运行您的项目。
回答by Torben
Since Alex Spurling's instructions are no longer valid for Eclipse Juno and the latest Android SDK, it may be useful to give another alternative:
由于 Alex Spurling 的说明对 Eclipse Juno 和最新的 Android SDK 不再有效,因此提供另一种替代方法可能会很有用:
- Create a separate ordinary Java library project that houses all the code that does not have dependencies to the Android platform.
- Write JUnit tests normally.
- Include that project to your Android project as a library reference.
- Use SLF4J or other high level library for logging.
- 创建一个单独的普通 Java 库项目,其中包含对 Android 平台没有依赖关系的所有代码。
- 正常编写 JUnit 测试。
- 将该项目作为库参考包含在您的 Android 项目中。
- 使用 SLF4J 或其他高级库进行日志记录。
It's a bit of a hassle on small projects, but OTOH it helps to enforce modularity and interface segregation.
这在小型项目上有点麻烦,但 OTOH 它有助于强制执行模块化和接口隔离。