Skip to content

Instantly share code, notes, and snippets.

@vkrm26
Last active April 2, 2021 09:10
Show Gist options
  • Save vkrm26/e2b35c2788c41c7248cd57226fe8ef28 to your computer and use it in GitHub Desktop.
Save vkrm26/e2b35c2788c41c7248cd57226fe8ef28 to your computer and use it in GitHub Desktop.
Robolectric test using custom shadow class
@RunWith(RobolectricTestRunner.class)
@Config(sdk = {VERSION_CODES.P}, shadows = {ShadowAdvertisingIdClient.class})
public class AdIdTest {
private Context context = ApplicationProvider.getApplicationContext();
@Test
public void test_when_context_is_not_null_then_get_advertising_id() {
AdvertisingIdClient.Info adId = new AdIdHelper(context).getAdvertisingID(context);
assertThat(adId).isNotNull();
assertThat(adId.getId()).isEqualTo("dsf0e47-90s4-4d5d-4949-6d0ba49dc350");
}
}
----------------------------------------
@Implements(AdvertisingIdClient.class)
public class ShadowAdvertisingIdClient {
public static Info getAdvertisingIdInfo(Context var0) throws IOException, IllegalStateException,
GooglePlayServicesNotAvailableException, GooglePlayServicesRepairableException {
return new Info("dsf0e47-90s4-4d5d-4949-6d0ba49dc350", false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment