Robolectric测试抛出RuntimeException:java.lang.ClassNotFoundException

时间:2015-12-18 11:29:52

标签: java android unit-testing android-studio robolectric

我用Robolectric测试编写了简单的hello-world。

我已经添加了build.gradle正确的依赖项:

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

这是我要测试的简单CartModel.java

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

点击“运行测试”后: enter image description here

我遇到此例外的测试失败:

  

java.lang.RuntimeException:java.lang.ClassNotFoundException:找不到包的类:和类名:com.android.tools.fd.runtime.BootstrapApplication       在org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:61)       在org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:15)       在org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:102)       在org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)       在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:240)       在org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)       在org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:152)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.junit.runners.Suite.runChild(Suite.java:128)       在org.junit.runners.Suite.runChild(Suite.java:27)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.junit.runner.JUnitCore.run(JUnitCore.java:137)       at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)       在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)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   引起:java.lang.ClassNotFoundException:无法找到包的类:klogi.com.dummyapp和类名:com.android.tools.fd.runtime.BootstrapApplication       at org.robolectric.internal.ClassNameResolver.resolve(ClassNameResolver.java:25)       在org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:59)       ......还有30多个

我运行Android Studio 2.0 Preview 3b。

问题是:如何避免失败?

1 个答案:

答案 0 :(得分:6)

这很奇怪,但我所要做的就是禁用即时运行(首选项 - >构建,执行,部署 - >即时运行 - >启用即时运行 - 取消选中)在Android Studio中。

enter image description here

现在所有的测试都在通过。