Mauricio Mauricio - 1 month ago 20
Android Question

Robolectric shadow not working

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?

Answer

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/