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

喜夏-厌秋 提交于 2019-11-26 21:56:00
Alex Spurling

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!