Monday, 28 October 2013

Testing with Robolectric (I)

If you have your own Application class for your Android app, you might have found your tests failing because of a class cast exception:
java.lang.ClassCastException: android.app.Application cannot be cast to BLALBLA class
And if you are using Robolectric 2.+ the way to solve this is to annotate your test with
@Config
like so:
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Test
@Config(manifest="src/main/AndroidManifest.xml")
public void shouldTestSomethingAmazing() throws Exception {
// Awesome code here
}
}

You can annotate also annotate the whole suite.