开发者

Using JMock on the Android

How can I use JMock on the Android? I've several posts saying its not possible, but surely there's some way to do it?

The issue seems to be getting the Android to even recognize t开发者_开发技巧he JMock jar file. So maybe there's a solution with putting the jar into assets and making a custom class loader? That seems like a lot of trouble, but does it sound like it would work?


I have JMock working inside Android opensource test projects.

Yes: JMock code executes on the device in your hand ;-)

I've used:

  • jmock-2.5.1.jar
  • Repackaged hamcrest-all-1.1.jar (I'll explain)
  • jmock-junit3-2.5.1.jar

See the following for a Android JMock Test case http://github.com/olibye/ToneDial/blob/master/ToneDialTest/src/net/xpdeveloper/dialer/test/TestDialServiceTest.java

Here are the three issues I had to overcome:


Hamcrest Packaging

The multiple packaging of Hamcrest fails Android package validation.

The error you get in Android ADK is:- Error generating final archive: duplicate entry: LICENSE.txt

Solution

unzip hamcrest-core-1.1.jar unzip hamcrest-library-1.1.jar in the same directory.

This the second unzip overwrites MANIFEST.MF and LICENCE.TXT.

zip ../hamcrest-all-1.1.jar

Initially I raised this with Steve and Nat for JMock

  • http://jira.codehaus.org/browse/JMOCK-242

However it's really Joe's packaging issue in Hamcrest (so I just posted it there ;-)

  • http://code.google.com/p/hamcrest/issues/detail?id=125

JUnit Packaging

The JUnit plugin in eclipse contains a subset of Hamcrest.

Solution You need to move the JUnit library down your classpath after JMock, in the eclipse project properties.


Android Packaging

JUnit v3 is part of android.jar so you can't use JUnit 4 style JMock tests. No annotations for us yet ;-)


Times have changed and @byeo's answer is more complex than necessary due to things being fixed and improved.

With Android Studio 3.1.4, I was able to get JMock and Hamcrest working easily, at least in Unit Tests. I added the following dependencies to my build.gradle file:

dependencies{
    ....
    testImplementation files('../../../../lib/jmock-2.8.3/jmock-2.8.3.jar')
    testImplementation files('../../../../lib/jmock-2.8.3/jmock-junit4-2.8.3.jar')
    testImplementation files('../../../../lib/jmock-2.8.3/jmock-legacy-2.8.3.jar')
    testImplementation files('../../../../lib/jmock-2.8.3/objenesis-2.1.jar')
    testImplementation files('../../../../lib/jmock-2.8.3/hamcrest-core-1.3.jar')
    testImplementation files('../../../../lib/jmock-2.8.3/hamcrest-library-1.3.jar')
    testImplementation files('../../../../lib/cglib-nodep-3.2.5.jar')
    ....
}

However, I'm unsure if JMock will work correctly in tests that use the Emulator or real hardware because of this question:

JMock jars do not work inside Android test project (project doesn't build )

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜