I am using Robolectric
for my Unit testing and updated my Robolectric
jar from 1.2 to 2.2 and trying to figure out how to bind shadow classes in this new version. This is what I was doing before:
Robolectric.bindShadowClass(ShadowLog.class);
@Implements(Log.class)
public static class ShadowLog {
public static int i(java.lang.String tag, java.lang.String msg) {
System.out.println("[" + tag + "] " + msg);
return 0;
}
}
But I think now there is no bindShadowClass API available. I tried using addShadowClass but I am not sure if this is the right way to add a shadow class. Can I just use
ShadowMap a = new ShadowMap.Builder().addShadowClass(ShadowLog.class).build();
Do I need to create a classHandler or something using this shadowMap and if yes, how do I create and use that classHandler to get access to my Log class methods?
@Implements(Log.class)
public static class ShadowLog {
public static int i(java.lang.String tag, java.lang.String msg) {
System.out.println("[" + tag + "] " + msg);
return 0;
}
}
And then Log.i("LogTest", "log message ");
Thanks Abhi
Robolectric has an important concept called shadows. Shadows are classes that modify or extend the behavior of classes in the Android SDK. Most of the Android core views are built with shadow classes to avoid needing the device or an emulator to run.
Students submit their assignments and take tests and exams on campus, but instead of actually attending classes at the university or college, they instead they turn to the shadow course where they can attend lectures in their preferred language.
The binding of shadow classes is now replaced with @Config
annotations.
Example:
@Config(shadows = {ShadowLog.class})
See also my answer to this other question and the Robolectric blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With