Java – Robolectric test throws RuntimeException : java. lang. ClassNotFoundException

Robolectric test throws RuntimeException : java. lang. ClassNotFoundException… here is a solution to the problem.

Robolectric test throws RuntimeException : java. lang. ClassNotFoundException

I wrote a simple hello-world test with Robolectric.

I’ve added the appropriate dependencies to build.gradle:

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

This is a simple CartModel .java I’m going 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);
    }
}

After you click Run Test:
enter image description here

I’m getting an exception where the test failed :

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’m running Android Studio 2.0 Preview 3b.

The question is: how to avoid failure?

Solution

It’s weird, but all I have to do is disable Instant Run in Android Studio (preferences -> Build, execute, deploy -> Instant Run -> Enable Instant Run – unchecked).

enter image description here

Now all tests have passed.

Related Problems and Solutions