I am having problem running shadow classes code in Robolectric. Or maybe my understanding of shadows is wrong. I have created a custom shadow.
This is the class that I am shadowing. Using robolectric-3, Android studio and gradle
public class MyConfig {
public static final String TEST = "test";
public static boolean testMethod() {
return false;
}
public boolean returnVal() { return false;}
}
This is the custom shadow class.
@Implements(value = MyConfig.class, isInAndroidSdk = false)
public class ShadowConfig {
public static final String TEST = "test";
@Implementation
public static boolean testMethod() {
return true;
}
@Implementation
public boolean returnVal() { return true;}
}
The ShadowConfig is in src/test/java folder and MyConfig is in src/main/java folder.
This is my test case.
@RunWith(RobolectricGradleTestRunner.class)
@Config(emulateSdk = 21, constants = BuildConfig.class, shadows = {ShadowConfig.class})
public class MyTest {
@Test
public void myActivityAppearsAsExpectedInitially() {
MyConfig config = new MyConfig();
assertTrue("Test boolean", MyConfig.testMethod());
assertTrue("Test boolean", config.returnVal());
}
}
If I have shadow implementation added to the test case, then it should run the methods of ShadowConfig instead of MyConfig class. But in this case,
its runnning the code of MyConfig class. Can anyone tell if my understanding is incorrect or I am doing anything wrong.