Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add shadow class when using Robolectric 2.2?

Tags:

robolectric

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

like image 991
Abhi Avatar asked Jun 18 '13 18:06

Abhi


People also ask

What are shadows in Robolectric?

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.

What are shadow classes?

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.


1 Answers

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.

like image 140
Bernd S Avatar answered Dec 28 '22 23:12

Bernd S