开发者

How to use JUnit for testing classes in Android app without using Android? [duplicate]

开发者 https://www.devze.com 2023-03-16 22:54 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Can't run JUnit 4 test case in eclipse
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消