3.0 to 3.1 Upgrade Guide

Christian Williams edited this page Jan 5, 2017 · 11 revisions

Changes

  • To construct Android components such as Activity, Service and ContentProvider classes you must now use the Robolectric APIs such as Robolectric.buildService(), Robolectric.buildActivity() or Robolectric.buildContentProvider() or the setup*() variants; you should not create new instances of these classes yourself. Calling these methods will create an instance of the component and attach its base context. This is now necessary as we've removed code shadowing Context and ContextWrapper in favor of using real framework code to improve fidelity.
Robolectric.buildService(MyService.class).create().get();
Robolectric.setupContentProvider(MyContentProvider.class);
  • We've removed shadow methods where they duplicate the functionality of the Android APIs. In general, prefer calling Android framework APIs over Robolectric shadows where possible.
3.0 3.1
ShadowApplication.getInstance().getContentResolver() RuntimeEnvironment.application.getContentResolver()
ShadowApplication.getInstance().getPackageManager() RuntimeEnvironment.application.getPackageManager()
ShadowApplication.getInstance().getResources() RuntimeEnvironment.application.getResources()
ShadowApplication.getInstance().getString() RuntimeEnvironment.application.getString()
ShadowApplication.getInstance().resetResources() RuntimeEnvironment.application.resetResources()
ShadowApplication.getInstance().getAssets() RuntimeEnvironment.application.getAssets()
ShadowPreferenceManager.getDefaultSharedPreferences() PreferenceManager.getDefaultSharedPreferences()
shadowOf(shadowOf(notification).getStyle()).getSummaryText() shadowOf(notification).getContentText()
  • RoboMenu now requires a context passed into its constructor. Previously it was internally using RuntimeEnvironment.application which you can use as a sensible default.

  • .equals() and .hashCode() methods have been removed from shadows to stop them incorrectly providing an alternative equality of the shadowed object. For example, Intent.equals() now behaves as the framework method. Instead of relying on an generically shadowed equals() method with a vague equality rule, prefer to make assertions on specific fields of interest to the test. We recommend using https://github.com/square/assertj-android to make assertions clear.

  • Custom shadows now require the public access modifier on methods in the shadow class.

3.1.1 Changes

  • RoboAttributeSet is deprecated and uses should be replaced with Robolectric.buildAttributeSet().
  • RobolectricGradleTestRunner is deprecated and uses should be replaced with RobolectricTestRunner.