3
votes

I've wrote simple hello-world with Robolectric test.

I've added to build.gradle proper dependencies:

testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.0"

Here's my simple CartModel.java to test:

public class CartModel {
    public float totalAmount;
    public int products;

    public void addToCart(float productPrice) {
        products++;
        totalAmount += productPrice;
    }
}

CartModelTest.java

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk=21)
public class CartModelTest {

    @Test
    public void addToCart() throws Exception {
        CartModel cartModel = new CartModel();
        assertEquals(0, cartModel.totalAmount, 0);
        assertEquals(0, cartModel.products);
        cartModel.addToCart(10.2f);
        assertEquals(10.2f, cartModel.totalAmount, 0);
        assertEquals(1, cartModel.products);
    }
}

Once I click to Run the test: enter image description here

I get a test failure with this exception:

java.lang.RuntimeException: java.lang.ClassNotFoundException: Could not find a class for package: and class name: com.android.tools.fd.runtime.BootstrapApplication at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:61) at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:15) at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:102) at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.ClassNotFoundException: Could not find a class for package: klogi.com.dummyapp and class name: com.android.tools.fd.runtime.BootstrapApplication at org.robolectric.internal.ClassNameResolver.resolve(ClassNameResolver.java:25) at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:59) ... 30 more

I run Android Studio 2.0 Preview 3b.

The question is: how to avoid the failure?

1

1 Answers

6
votes

That's pretty bizarre, but all I had to do - is to disable instant run (Preferences -> Build, Execution, Deployment -> Instant Run -> Enable Instant Run - uncheck) in Android Studio.

enter image description here

Now all tests are passing.