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?
I have JMock working inside Android opensource test projects.
Yes: JMock code executes on the device in your hand ;-)
I've used:
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:
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 ;-)
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With