Can't run JUnit 4 test case in Eclipse Android project
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:
- 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.
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:
- 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.
It's a bit of a hassle on small projects, but OTOH it helps to enforce modularity and interface segregation.