0

I am getting "java.lang.RuntimeException: Stub!" when running the roboelectric tests.I have proper eclipse ordering and also set the runner properly. Here's the stack trace.

java.lang.RuntimeException: java.lang.RuntimeException: Stub!
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Stub!
at android.net.Uri.parse(Uri.java:53)
at org.robolectric.shadows.ShadowMediaStore.reset(ShadowMediaStore.java:27)
at org.robolectric.Robolectric.reset(Robolectric.java:1351)
at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:47)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:226)
... 16 more

here is my test

  package com.example.sample.test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import com.example.sample.MainActivity;
import com.example.sample.R;

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
    private MainActivity activity;

    @Test
    public void shouldHaveHappySmiles() throws Exception {
        String hello = new MainActivity().getResources().getString(R.string.hello_world);
        assertThat(hello, equalTo("Hello world!"));
        activity = Robolectric.buildActivity(MainActivity.class)
                .create().get();
    }
}

I wrote this testcase just to hang around with Roboelectric.

3
  • You are running as JUnit tests, not Android tests, right? Commented Nov 10, 2014 at 9:52
  • im running as Junit4 tests Commented Nov 10, 2014 at 10:58
  • Could you please show the code for a simple test you are trying to run? Commented Nov 10, 2014 at 11:40

1 Answer 1

2

You shouldn't do :

new MainActivity().getResources().getString(R.string.hello_world);

This is probably where the Stub! exception comes from, although it's not clear in the stack trace.

Instead, create your activity with Robolectric, then get the value of the hello string, then assert:

activity = Robolectric.buildActivity(MainActivity.class).create().start().resume().visible().get();
String hello = Robolectric.shadowOf(activity).getResources().getString(R.string.hello_world);
assertThat(hello, equalTo("Hello world!"));

You can also use ShadowApplication#getResources().

Sign up to request clarification or add additional context in comments.

2 Comments

the error persists even after changing as u mentioned.
I've updated a bit my answer. I usually create the activity by calling all the methods of the lifecycle (create, start, resume etc). Then I changed the way to load the resources, using the ShadowActivity of the created activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.