How to fix problem when app won’t start when testing with calabash-android

I’ve been starting to use calabash-android, which is a way to run cucumber tests on Android. It requires Ruby Gems and Xcode Command Line Tools on Mac, which installed fine. However, when I ran the first sample test, then I noticed the app did not start. I looked at the output and saw that this error showed up:

App did not start (RuntimeError)
/Library/Ruby/Gems/1.8/gems/calabash-android-0.4.3/lib/calabash-android/operations.rb:364:in `start_test_server_in_background'

I looked around for the solution and did not find, so I tried a few things. It turns out to be an easy fix. In your Android app’s manifest, aka AndroidManifest.xml, include the line:

<uses-permission android:name="android.permission.INTERNET" />

inside of the manifest tag, after your application tag.

Why does this work? The reason is because Calabash-Android (and Cucumber in general) uses HTTP to communicate between the host computer and the target application. Since Android apps do not have permission to communicate via HTTP by default, the test fails to start.

java.lang.ExceptionInitializerError when using Mockito on Android

So you’re being a good Android developer, using JUnit 3 tests [Sidenote: JUnit 4 does not play well with the JUnit runner in Eclipse for Android currently] and using Mockito to create mocks to make sure you’re focusing on the class that you’re testing. However, you run into a java.lang.ExceptionInitializerError when running your tests. This is pictured below. What should you do?

tl;dr –

To use Mockito on a device or emulator, you’ll need to add three .jar files to your test project’s libs directory: mockito-all-1.9.5.jar, dexmaker-1.0.jar, and dexmaker-mockito-1.0.jar.

“Mockito on Android” from the Square Engineering Blog

Put those 3 JARs linked above in a “libs” directory in your test project.

The root cause of this is that the classes that Mockito makes needs to interact with dex, which is what Android uses to create its classes. This can be done with Dexmaker and the Dexmaker/Mockito integration. Once you add in those 3 JARs into your libs directory, just do the refresh/clean project dance and then you should be good to go.

Special thanks to Jesse Wilson (@jessewilson) for making DexMaker and integrating Mockito with Android!