13

I'm trying to create a test with Robolectric. My goal is to be able to replace the functionality of one class (that comes for example from a library and I can't modify the code) from a custom behaviour.

I created this small test to simulate what I want to do:

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowMessenger.class})
public class TestShadow {

    @Test
    public void testMessenger() {
        OriginalMessenger messenger = new OriginalMessenger();
        String message = messenger.getMessage();
        Assert.assertEquals("Shadow messenger", message);
    }

    public static class OriginalMessenger {

        public String getMessage() {
            return "Original messenger";
        }
    }

    @Implements(OriginalMessenger.class)
    public static class ShadowMessenger extends OriginalMessenger {

        @Implementation
        public String getMessage() {
            return "Shadow messenger";
        }
    }
}

In the example, OriginalMessenger is the class that is in the library and provides a default functionality. And ShadowMessenger is the class that contains the custom behaviour that I want to apply whenever I use OriginalMessenger.

However when I run the test it fails. The content of message is "Original messenger". As if the ShadowMessenger was never used.

What am I doing wrong?

  • 2
    Why make a shadow? Why not `mock` or `stub` it out? – Jared Burrows Apr 14 '15 at 22:16
  • How do I achieve the same result using mocks and stubs? – Mauricio Togneri Apr 17 '15 at 19:33
  • Oh I see nenick solved your problem? You can use EasyMock/PowerMock to mock out of your class or Spock(Robospock). Check out an example here: https://github.com/jaredsburrows/BurrowsAppsExamples/blob/master/Lib-BurrowsApps/src/test/java/burrows/apps/example/lib/utils/PlayServicesUtilsTest.java. – Jared Burrows Apr 17 '15 at 19:38
  • If I apply nenick's answer I get an StackOverflowError, I don't know why. I can't make it run with PowerMock. I get: `Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath`. I took the dependencies from the example. Thanks for the help. – Mauricio Togneri Apr 17 '15 at 20:24
  • are you using "testCompile"? Did you setup the annotations? – Jared Burrows Apr 18 '15 at 14:00
  • Yes. I have these: testCompile 'org.powermock:powermock-module-junit4:1.6.2' testCompile 'org.powermock:powermock-api-easymock:1.6.2' testCompile 'org.powermock:powermock-core:1.6.2' testCompile 'org.easymock:easymock:3.3.1' Here is the code: https://github.com/mauriciotogneri/android-studio-template/tree/master/app/src/test/java/com/example – Mauricio Togneri Apr 19 '15 at 15:47
  • There are many ways of doing this. If your class is a simple pojo, extending it and overriding the one method would be fine for a `stub` or `dummy` class for testing. If your class requires internet or a database, it would be a good idea to `mock` or `spy` it and make it "return" output you were expecting. – Jared Burrows Apr 19 '15 at 15:51
  • I don't know if I can apply that in my case. I have a class `A` that I want to test. `A` needs the class `B` to work. But `B` is in a library so I can't modify it or extend it. I want to test `A` but without using the original `B`. I want to use a fake `B` without changing the code of `A`. That's why I thought I could use shadows. My idea was to say: whenever the code requires `B`, let's use fake `B` instead. One possible solution could be using dependency injection, where I inject `B` (or fake `B`) into `A` to be used. But I wanted to know if it's possible with shadows. – Mauricio Togneri Apr 19 '15 at 21:04
  • Is `OriginalMessenger` open source? Based on your example, it really only looks like you are wanting `getMessage()` to be overwritten. – Jared Burrows Apr 19 '15 at 21:37
  • That code just belongs to a sample project. It's not related to the actual code that I want to test (I can't publish it). I made the scenario with the OriginalMessenger/ShadowMessenger just to practice shadows to see if it was possible what I wanted to achieve. If I solve it in the messengers scenario then I will translate it to the original project. – Mauricio Togneri Apr 19 '15 at 22:24
  • Alright, I have made my suggestions. – Jared Burrows Apr 19 '15 at 22:40

1 Answers1

16

Original you can only shadow android classes. But with a custom robolectric test runner you can also shadow your own classes.

Robolectric 3.1.4 (RobolectricGradleTestRunner was completely removed, so you need to override method described below in RobolectricTestRunner)

@Override
protected ShadowMap createShadowMap() {
    return new ShadowMap.Builder()
        .addShadowClass(OriginalMessenger.class, ShadowMessenger.class, true, true, true)
        .build();
}

Robolectric 3.0

@Override
public InstrumentationConfiguration createClassLoaderConfig() {
    InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
    builder.addInstrumentedClass(OriginalMessenger.class.getName());
    return builder.build();
}

Robolectric 2.4

@Override
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
    return super.createRobolectricClassLoader(new ExtraShadows(setup), sdkConfig);
}

class ExtraShadows extends Setup {
    private Setup setup;

    public ExtraShadows(Setup setup) {
        this.setup = setup;
    }

    public boolean shouldInstrument(ClassInfo classInfo) {
        boolean shoudInstrument = setup.shouldInstrument(classInfo);
        return shoudInstrument
                || classInfo.getName().equals(OriginalMessenger.class.getName());
    }
}

example project https://github.com/nenick/android-gradle-template/

nenick
  • 7,340
  • 3
  • 31
  • 23