简体   繁体   English

无法在Eclipse Android项目中运行JUnit 4测试用例

[英]Can't run JUnit 4 test case in Eclipse Android project

I am new to Java and am trying to run a unit test on a class I am writing. 我是Java的新手,我正在尝试对我正在编写的类进行单元测试。 Eclipse (3.5) created the unit test class for me and added Junit4 to my class path. 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: 当我右键单击单元测试并选择run as Junit Test时,我得到以下内容:

#
# 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 谢谢

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 comment thread: 以下是从此评论主题中采取的步骤:

  1. Right click on the project -> run -> run configuration 右键单击项目 - >运行 - >运行配置
  2. Select your Junit project 选择您的Junit项目
  3. Go to the classpath tab 转到类路径选项卡
  4. remove the Android framework entry 删除Android框架条目
  5. select bootstrap entries 选择bootstrap条目
  6. click on advanced 点击高级
  7. select Add Library 选择添加库
  8. Ok
  9. Chose "JRE System Library" 选择“JRE系统库”
  10. Next 下一个
  11. finish
  12. You need to also add the JUnit library so follow the steps 5 to 11 and select the "Junit" instead of "JRE System Library" 您还需要添加JUnit库,因此请按照步骤5到11并选择“Junit”而不是“JRE System Library”
  13. You can now run your project as Junit. 您现在可以将项目作为Junit运行。

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,因此提供另一种选择可能会有用:

  1. Create a separate ordinary Java library project that houses all the code that does not have dependencies to the Android platform. 创建一个单独的普通Java库项目,其中包含所有与Android平台无依赖关系的代码。
  2. Write JUnit tests normally. 正常编写JUnit测试。
  3. Include that project to your Android project as a library reference. 将该项目作为库引用包含在您的Android项目中。
  4. Use SLF4J or other high level library for logging. 使用SLF4J或其他高级库进行日志记录。

It's a bit of a hassle on small projects, but OTOH it helps to enforce modularity and interface segregation. 这对小型项目来说有点麻烦,但OTOH有助于强化模块化和界面隔离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM