开发者

Android Junit test - Mockito class not found exception

I'm attempting to build a unit test on Android which utilizes the mockito-all-1.8.5.jar file from mockito.org. I added the jar file to my project using project > properties > project build path > add external jar. This has always worked for me on non-Android java projects.

However, when I run the unit test, I get the following class not found exception:

java.lang.ClassNotFoundException: org.mockito.runners.VerboseMockitoJUnitRunner

Above it is the following message:

Cannot load class. Make sure it is in your apk. Class name: 'org.mockito.runners.VerboseMockitoJUnitRunner'. Message: org.mockito.runners.VerboseMockitoJUnitRunner

I checked the jar and the class is there.

I also tried adding according to this link:

How to use and package a JAR file with my Android app?

But, still no luck. How can I get this class to be found?

Edit: I'm thinking there is a problem with whatever libraries Mockitto depends on - they may not be compatible with the Dalvik jvm. See this post:

http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/

1) Import a mocking framework (e.g. mockito) into the project as an additional dependency. Any imported jars containing class files not compiled to Dalvik bytecode (most) will not work. Attempting to compile the source along with your project will not work either because most libraries will make extensive use of parts of the Java language not compatible with Dalvik: it uses its own library built o开发者_Go百科n a subset of the Apache Harmony Java implementation.


If you are executing inside the Dalvik JVM you won't be able to use Mockito.

If you are running your unit tests on your desktop JVM however (with a testing framework like Robolectric for example) then you are good to go. I am combining the two on my Android projects at present, to good effect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜