views:

36

answers:

1

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 the 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?

+1  A: 

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

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


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 ;-)

Oli Bye