Settings.Secure.getString(Robolectric.application.getContentResolver(), Settings.Secure.ANDROID_ID) returns Null

1,416 views
Skip to first unread message

lukasz...@miquido.com

unread,
Mar 24, 2015, 10:23:27 AM3/24/15
to robol...@googlegroups.com
Hi

I am completely new to Robolectric to say it bluntly and am trying to apply it to an existing application. The application retrieves the Device ID during Application object initialization, through:
Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)

When I debug the app on device the returned value is not null (obviously). But I can't figure out what to do to bypass it with Robolectric. As far as I know, Robolectric runs the tests without the need of a real device or emulator, so it would make sense the Device ID is null in this case (correct?), but what can I do in this case. What would be your recommendation?

Lucas

lukasz...@miquido.com

unread,
Mar 24, 2015, 10:35:50 AM3/24/15
to robol...@googlegroups.com
Forgot to mention that I'm using Robolectric 2.4.

Erich Douglass

unread,
Mar 24, 2015, 4:23:08 PM3/24/15
to robol...@googlegroups.com
You can set the value to something known to the test:

   Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ANDROID_ID, "foo")

But if you're retrieving the value in your Application's onCreate, it may not be possible to insert the value in your test (since you don't yet have access to a context).

--
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.

lukasz...@miquido.com

unread,
Mar 26, 2015, 7:05:02 AM3/26/15
to robol...@googlegroups.com
Thank you Erich. That's exactly right, we're retrieving this information in the chain that is essentialy started in onCreate of Application object. Is there any way I can bypass it, without putting some conditional logic inside the app's code? I've seen some Shadow functionality with Robolectric, maybe this could help?

sincerely
Lucas

Luis Fernandez

unread,
Mar 30, 2015, 9:38:40 AM3/30/15
to robol...@googlegroups.com
Hi all,

Same situation here, retrieving the ANDROID_ID in the onCreate of the class that extends Application.
If I find a way to test it I'll post in round here. My efforts are pointing towards adding a ShadowContextImpl with the Settings.Secure inside.
No luck so far.

Jonathan Gerrish

unread,
Mar 30, 2015, 10:59:18 AM3/30/15
to robol...@googlegroups.com

Maybe we need to separate setting up the environment from starting the application?

Android would new your application and call the life cycle methods when it starts a component in your app. I wonder if it would be too disruptive to do the same in Robolectric?

Otherwise we could leave it as is and add something like @BeforeApplicationOnCreate maybe?

Reply all
Reply to author
Forward
0 new messages