gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:56 25 4
gpt4 key购买 nike

我用 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: 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

我运行的是 Android Studio 2.0 Preview 3b。

问题是:如何避免失败?

最佳答案

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

enter image description here

现在所有测试都通过了。

关于java - Robolectric 测试抛出 RuntimeException : java. lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com