簡體   English   中英

Robolectric測試拋出RuntimeException:java.lang.ClassNotFoundException

[英]Robolectric tests throw RuntimeException: java.lang.ClassNotFoundException

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

一旦我點擊運行測試: 在此輸入圖像描述

我遇到這個例外的測試失敗:

java.lang.RuntimeException:java.lang.ClassNotFoundException:找不到包的類:和類名:com.android.tools.fd.runtime.BootstrapApplication at 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)at 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的org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) .runners.Pare ntRunner.access $ 000(ParentRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)atg.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)atg.junit .runners.ParentRunner $ 3.run(ParentRunner.java:290)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at在org.junit.runners.ParentRunner.run(ParentRunner.java: 363)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)的com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart上的org.junit.runner.JUnitCore.run(JUnitCore.java:137) (JUnitStarter.java:234)在co m.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。在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)的java.lang.reflect.Method.invoke(Method.java:606)中反映.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)引起:java.lang.ClassNotFoundException:找不到包的類:klogi.com.dummyapp和類名:com.android.tools.fd.runtime.BootstrapApplication at org.robolectric.internal.ClassNameResolver.resolve(ClassNameResolver。 java:25)at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:59)... 30更多

我運行Android Studio 2.0 Preview 3b。

問題是:如何避免失敗?

這非常奇怪,但我所要做的就是在Android Studio中禁用即時運行 (首選項 - >構建,執行,部署 - >即時運行 - >啟用即時運行 - 取消選中)。

在此輸入圖像描述

現在所有測試都在通過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM