Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Eclipse (3.5) created the unit test class for me and added Junit4 to my class path.

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:

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

like image 642
Roaders Avatar asked Jan 31 '10 15:01

Roaders


People also ask

Why does Eclipse not run JUnit?

The error occurs because the JUnit library has not been configured for the project, but can be resolved using the following steps. 1. Right click on the Java project and select Build Path > Configure Build Path. 2.

How do I run JUnit test cases without Eclipse?

gradle / pom. xml . @SSharma For example, if you want to build JUnit 4 (which uses Maven) or JUnit 5 (which uses Gradle) from source code yourself, download the project from GitHub and just run mvnw ( .


2 Answers

Having searched Google for an answer, this looks like it might have something to do with Android development tools.

Below are steps taken from this comment thread:

  1. Right click on the project -> run -> run configuration
  2. Select your Junit project
  3. Go to the classpath tab
  4. remove the Android framework entry
  5. select bootstrap entries
  6. click on advanced
  7. select Add Library
  8. Ok
  9. Chose "JRE System Library"
  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"
  13. You can now run your project as Junit.
like image 120
Alex Spurling Avatar answered Oct 09 '22 04:10

Alex Spurling


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:

  1. Create a separate ordinary Java library project that houses all the code that does not have dependencies to the Android platform.
  2. Write JUnit tests normally.
  3. Include that project to your Android project as a library reference.
  4. Use SLF4J or other high level library for logging.

It's a bit of a hassle on small projects, but OTOH it helps to enforce modularity and interface segregation.

like image 28
Torben Avatar answered Oct 09 '22 04:10

Torben