Custom Robolectric Runner

209 views
Skip to first unread message

Jan Carlo

unread,
Nov 24, 2014, 4:28:11 AM11/24/14
to robol...@googlegroups.com
Hi!

I have a project that requires creation of a custom Robolectric runner that will inject this custom Context class:

@Implements(Context.class)

public class CustomContext
{

     
private final SharedPreferences preferences = new TestSharedPreferences(new HashMap<String, Map<String, Object>>(), "__default__", Context.MODE_PRIVATE);

     
@Implementation
     
public SharedPreferences getSharedPreferences(String name, int mode)
     
{
           
System.out.println("ShadowSharedPreferenceSavableContext");
           
return preferences;
     
}

}


I'm just starting to learn Robolectric. I hope that you can help me with my problem. Thank you


Carlo

Victor Ng

unread,
Nov 25, 2014, 9:07:12 AM11/25/14
to robol...@googlegroups.com
I don't understand your question.

Why don't you just use a ContextWrapper?

vic

Jan Carlo

unread,
Nov 26, 2014, 7:08:25 PM11/26/14
to robol...@googlegroups.com
How am I going to use that? I already tried searching documents on how to use it but I failed.
Message has been deleted

Jan Carlo

unread,
Nov 26, 2014, 11:35:14 PM11/26/14
to robol...@googlegroups.com
I wanted to ask on how to create a custom runner that can inject the custom Context class. I'm hoping that you can help me. Thank you for your replies

Victor Ng

unread,
Nov 27, 2014, 8:48:17 AM11/27/14
to robol...@googlegroups.com
It's still not clear what you're trying to do.  You need to post a clear description of what you are trying to accomplish with some code that people can read to get an understanding of what you are describing.

On Wed, Nov 26, 2014 at 11:35 PM, Jan Carlo <jancarl...@gmail.com> wrote:
I wanted to ask on how to create a custom runner that can inject the custom Context class. I'm hoping that you can help me. Thank you for your replies

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Carlo

unread,
Dec 2, 2014, 8:47:40 PM12/2/14
to robol...@googlegroups.com
I need to inject the custom shadow so I can use it whenever I will call on context.getSharedPreferences()
Here is my code so far, no errors found but the string "ShadowSharedPreferenceSavableContext" didn't show at all

public class CustomTestRunner extends RobolectricTestRunner {

private static final List<String> CUSTOM_SHADOW_TARGETS = Collections.unmodifiableList(Arrays.asList("com.myPackage.android.CustomContext"));

public CustomTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}

@Override
protected ShadowMap createShadowMap() {
return super.createShadowMap().newBuilder().addShadowClass(CustomContext.class).build();
}

@Override
public Setup createSetup() {
return new CustomSetup();
}

public class CustomSetup extends Setup {

@Override
public boolean shouldInstrument(ClassInfo classInfo) {
return CUSTOM_SHADOW_TARGETS.contains(classInfo.getName()) || super.shouldInstrument(classInfo);
}
}

}

Erich Douglass

unread,
Dec 3, 2014, 1:22:08 PM12/3/14
to robol...@googlegroups.com
You shouldn't need to create a custom runner or use a custom Context implementation (the latter would most certainly cause problems with Robolectric) to add shared preferences. You should be able to call PreferenceManager.getDefaultSharedPreferences(Robolectric.application) and add/remove things just like you would in an application. Unfortunately, like others have said, it's hard to provide more specific information unless you can tell us what you're trying to test.

Reply all
Reply to author
Forward
0 new messages