Domanda

I have a Java class called Warehouse which is using Android's Bundle. When trying to unit test it, I get the following exception:

java.lang.RuntimeException: Stub!

The class looks like that:

public class Warehouse {
    private Bundle items;

    public Warehouse(Bundle items) {
        this.items = items;
    }
}

In the unit test I have the following:

void testInitializeWarehouse() {
     Bundle items = new Bundle();
     Warehouse wh = new Warehouse(items);
     assertNotNull(wh);
}

When I run the test I get java.lang.RuntimeException: Stub! I know this issue is related to me running the unit test on JVM rather than Dalvik VM, but is there a way to run this unit test on JVM?

È stato utile?

Soluzione 2

I found out Robolectric, which is exactly what I was looking for. I could have used mocks but then half of the unit tests will consist of mocks setups etc.

"a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds."

Altri suggerimenti

Try using Powermock to mock Bundle using its ability to mock a final class as described here - Powermock MockFinal

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top