How to use JUnit for testing classes in Android app without using Android? [duplicate]
Possible Duplicate:
Can't run JUnit 4 test case in eclipse
I have an Android application, which contains plain Java class. I want to test this class without using Android device or Emulator, but when I create JUnit 4 test suite and case & run int (as JUnit test in Eclipse), it says the following:
# A fatal error has been detected by the Java R开发者_开发问答untime Environment:
#
# Internal Error (classFileParser.cpp:3494), pid=5492, tid=5608
# Error: ShouldNotReachHere()
What should I do?
If you want unit tests to run locally, you need to run your tests from a Java project (as opposed to an Android project). I think the reason you get that error is because you're running Dalvik bytecode on the JVM.
You can separate your java code into a java library that you unit test.
Or you can use Robolectric to test your code as is. Robolectric rewrites Android SDK classes so you can run them locally. The Eclipse quickstart is useful for getting started. You'll be creating a java junit project and not an Android test project.
An alternate approach to Robolectric is to use mock frameworks such as Mockito or Android Mock to mock out the Android SDK.
I would recommend to separate the "plain Java code" into an own library which can be tested independently from you android application. The solution is also recommended here (Maven: The Complete Reference).
This thread has some good information on unit testing on Android. And a link to Robolectric which might be an alternative you're looking for.
I think you need to use the AndroidTestRunner and the AndroidTestCase for JUnit on Android.
精彩评论