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/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…