Using JMock on the Android
Asked Answered
P

2

7

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?

Pascal answered 27/8, 2010 at 1:6 Comment(0)
O
8

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

Ovoviviparous answered 12/10, 2010 at 15:53 Comment(0)
X
1

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 )

Xylidine answered 23/10, 2018 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.