1
- ---
2
- title : Robolectric Migration Guide
3
- ---
4
-
5
- ## Automated Migration
6
-
7
- Robolectric provides an [ automated migration tool] ( automated-migration.md ) to help keep your test
8
- suite up to date with Robolectric API changes.
9
-
10
- ## Migrating to 4.0
11
-
12
- ### Project Configuration
13
-
14
- Robolectric 4.0 requires Android Gradle Plugin 3.2 or greater.
15
-
16
- Update the configuration in your module's ` build.gradle ` /` build.gradle.kts ` file:
17
-
18
- /// tab | Groovy
19
- ``` groovy
20
- android {
21
- compileSdkVersion 28 // Or newer
22
- testOptions.unitTests.includeAndroidResources = true
23
- }
24
- ```
25
- ///
26
-
27
- /// tab | Kotlin
28
- ``` kotlin
29
- android {
30
- compileSdkVersion = 28 // Or newer
31
- testOptions.unitTests.isIncludeAndroidResources = true
32
- }
33
- ```
34
- ///
35
-
36
- Add the following in your ` gradle.properties ` file:
37
-
38
- ``` properties
39
- android.enableUnitTestBinaryResources =true
40
- ```
41
-
42
- If you have dependencies on ` com.android.support.test ` , switch them to ` androidx.test ` ; see
43
- [ Migrate to AndroidX] [ migrate-to-androidx ] .
44
-
45
- ### Deprecations
46
-
47
- | 3.8 | 4.0 |
48
- | --------------------------------------------| ---------------------------------------------------------------------------------------------------|
49
- | ` ShadowApplication.getInstance() ` | [ ` RuntimeEnvironment.application ` ] [ runtime-environment-application-javadoc ] |
50
- | ` ShadowApplication.getLatestAlertDialog() ` | [ ` ShadowAlertDialog.getLatestAlertDialog() ` ] [ shadow-alert-dialog-get-latest-alert-dialog-javadoc ] |
51
- | ` ShadowApplication.getLatestDialog() ` | [ ` ShadowDialog.getLatestDialog() ` ] [ shadow-dialog-get-latest-dialog-javadoc ] |
52
- | ` ShadowApplication.getLatestPopupMenu() ` | [ ` ShadowPopupMenu.getLatestPopupMenu() ` ] [ shadow-popup-menu-get-latest-popup-menu-javadoc ] |
53
- | ` ShadowLooper.getShadowMainLooper() ` | [ ` shadowOf(Looper.getMainLooper()) ` ] [ shadow-of-looper-javadoc ] |
54
-
55
- The [ automatic migration tool] ( automated-migration.md ) includes a migration to help with this.
56
-
57
- The following attributes of the [ ` @Config ` ] [ config-javadoc ] annotation are no longer supported when
58
- using binary resources mode:
59
-
60
- * [ ` assetDir ` ] [ config-asset-dir-javadoc ] and [ ` resourceDir ` ] [ config-resource-dir-javadoc ] : follow
61
- the recommended file structure of your build system.
62
- * [ ` manifest ` ] [ config-manifest-javadoc ] : Robolectric always uses the merged manifest generated by
63
- the Android toolchain. If your test was using a custom manifest you'll need to adapt it to not
64
- rely on that.
65
- * [ ` packageName ` ] [ config-package-name-javadoc ] : to change your package name, override the
66
- ` applicationId ` in your build system.
67
-
68
- ### Improper Use of Shadows
69
-
70
- Prior to Robolectric 4.0, it was possible (but ill-advised) to get the shadow for an Android
71
- framework object and invoke framework methods there. This could result in unexpected behavior (e.g.,
72
- code in overridden methods in subclasses wouldn't be called). Shadow implementation methods are now
73
- marked ` protected ` to guard against this. Always invoke framework methods directly on the Android
74
- class.
75
-
76
- | 3.8 | 4.0 |
77
- | ------------------------------------------| --------------------------------------------------------------------------|
78
- | ` shadowOf(activity).finish(); ` | [ ` activity.finish() ` ] [ activity-finish-documentation ] |
79
- | ` ShadowSystemClock.currentTimeMillis(); ` | [ ` System.currentTimeMillis() ` ] [ system-current-time-millis-documentation ] |
80
-
81
- The [ automatic migration tool] ( automated-migration.md ) will fix most of these for you.
82
-
83
- ### ` androidx.test `
84
-
85
- Robolectric 4.0 includes initial support for [ ` androidx.test ` APIs] [ androidx-test-apis ] . We strongly
86
- recommend adding the latest version of ` androidx.test:core ` as a test dependency and using those
87
- APIs whenever possible rather than using Robolectric-specific APIs.
88
-
89
- | 3.8 | 4.0 |
90
- | ----------------------------------| -------------------------------------------------------------------------------------------------------------|
91
- | ` RuntimeEnvironment.application ` | [ ` ApplicationProvider.getApplicationContext() ` ] [ application-provider-get-application-context-documentation ] |
92
- | ` ShadowMotionEvent ` | [ ` MotionEventBuilder ` ] [ motion-event-builder-documentation ] |
93
-
94
- ### Troubleshooting
95
-
96
- Robolectric 4.0 replaces its old home-grown resource handling code with a direct adaptation of
97
- Android's resource handling code, using the full Android toolchain. This greatly improves fidelity
98
- to the behavior of a real Android device, but if your tests were relying on the quirks of the old
99
- code, you may need to fix your tests.
100
-
101
- Some likely issues include:
102
-
103
- !!! quote ""
104
-
105
- > android.view.InflateException: Binary XML file line #3: Failed to resolve attribute at index
106
- > 17: TypedValue{t=0x2/d=0x7f01000e a=-1}
107
-
108
- This happens when your [`Activity`][activity-documentation] is using a theme that lacks values
109
- for certain attributes used by layouts. Make sure you've specified an appropriate theme for your
110
- activities in your `AndroidManifest`.
111
-
112
- ---
113
-
114
1
<!-- markdownlint-disable-next-line MD033 -->
115
2
## Migrating to 3.6<a name =" migrating-from-35-to-36 " ></a >
116
3
@@ -550,15 +437,9 @@ testCompile("org.robolectric:shadows-maps:3.0")
550
437
551
438
[ activity-controller-javadoc ] : javadoc/latest/org/robolectric/android/controller/ActivityController.html
552
439
[ activity-documentation ] : https://developer.android.com/reference/android/app/Activity
553
- [ activity-finish-documentation ] : https://developer.android.com/reference/android/app/Activity#finish()
554
- [ androidx-test-apis ] : https://developer.android.com/reference/androidx/test/package-summary
555
- [ application-provider-get-application-context-documentation ] : https://developer.android.com/reference/androidx/test/core/app/ApplicationProvider#getApplicationContext()
556
440
[ config-asset-dir-javadoc ] : javadoc/latest/org/robolectric/annotation/Config.html#assetDir()
557
441
[ config-javadoc ] : javadoc/latest/org/robolectric/annotation/Config.html
558
- [ config-manifest-javadoc ] : javadoc/latest/org/robolectric/annotation/Config.html#manifest()
559
442
[ config-merger-get-config-properties-javadoc ] : javadoc/latest/org/robolectric/ConfigMerger.html#getConfigProperties(java.lang.String)
560
- [ config-package-name-javadoc ] : javadoc/latest/org/robolectric/annotation/Config.html#packageName()
561
- [ config-resource-dir-javadoc ] : javadoc/latest/org/robolectric/annotation/Config.html#resourceDir()
562
443
[ content-provider-controller-javadoc ] : javadoc/latest/org/robolectric/android/controller/ContentProviderController.html
563
444
[ content-provider-documentation ] : https://developer.android.com/reference/android/content/ContentProvider
564
445
[ context-documentation ] : https://developer.android.com/reference/android/content/Context
@@ -573,9 +454,7 @@ testCompile("org.robolectric:shadows-maps:3.0")
573
454
[ fake-http-get-latest-sent-http-request-javadoc ] : javadoc/latest/org/robolectric/shadows/httpclient/FakeHttp.html#getLatestSentHttpRequest()
574
455
[ fragment-controller-javadoc ] : javadoc/latest/org/robolectric/android/controller/FragmentController.html
575
456
[ intent-service-controller-javadoc ] : javadoc/latest/org/robolectric/android/controller/IntentServiceController.html
576
- [ migrate-to-androidx ] : https://developer.android.com/jetpack/androidx/migrate
577
457
[ mockito ] : https://site.mockito.org/
578
- [ motion-event-builder-documentation ] : https://developer.android.com/reference/androidx/test/core/view/MotionEventBuilder
579
458
[ package-manager-documentation ] : https://developer.android.com/reference/android/content/pm/PackageManager
580
459
[ preference-manager-get-default-shared-preferences-documentation ] : https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context)
581
460
[ robo-executor-service-javadoc ] : javadoc/latest/org/robolectric/android/util/concurrent/RoboExecutorService.html
@@ -590,22 +469,17 @@ testCompile("org.robolectric:shadows-maps:3.0")
590
469
[ runtime-environment-application-javadoc ] : javadoc/latest/org/robolectric/RuntimeEnvironment.html#application
591
470
[ service-controller-javadoc ] : javadoc/latest/org/robolectric/android/controller/ServiceController.html
592
471
[ service-documentation ] : https://developer.android.com/reference/android/app/Service
593
- [ shadow-alert-dialog-get-latest-alert-dialog-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowAlertDialog.html#getLatestAlertDialog()
594
472
[ shadow-application-package-manager-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowApplicationPackageManager.html
595
- [ shadow-dialog-get-latest-dialog-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowDialog.html#getLatestDialog()
596
473
[ shadow-display-get-default-display-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowDisplay.html#getDefaultDisplay()
597
474
[ shadow-drawable-get-created-from-res-id-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowDrawable.html#getCreatedFromResId()
598
475
[ shadow-extract-javadoc ] : javadoc/latest/org/robolectric/shadow/api/Shadow.html#extract(java.lang.Object)
599
476
[ shadow-javadoc ] : javadoc/latest/org/robolectric/shadow/api/Shadow.html
600
477
[ shadow-notification-get-content-text-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowNotification.html#getContentText()
601
478
[ shadow-notification-is-indeterminate-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowNotification.html#isIndeterminate()
602
479
[ shadow-of-drawable-javadoc ] : javadoc/latest/org/robolectric/Shadows.html#shadowOf(android.graphics.drawable.Drawable)
603
- [ shadow-of-looper-javadoc ] : javadoc/latest/org/robolectric/Shadows.html#shadowOf(android.os.Looper)
604
480
[ shadow-of-notification-javadoc ] : javadoc/latest/org/robolectric/Shadows.html#shadowOf(android.app.Notification)
605
481
[ shadow-of-package-manager-javadoc ] : javadoc/latest/org/robolectric/Shadows.html#shadowOf(android.content.pm.PackageManager)
606
482
[ shadow-package-manager-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowPackageManager.html
607
- [ shadow-popup-menu-get-latest-popup-menu-javadoc ] : javadoc/latest/org/robolectric/shadows/ShadowPopupMenu.html#getLatestPopupMenu()
608
483
[ shared-preferences-documentation ] : https://developer.android.com/reference/android/content/SharedPreferences
609
484
[ square-assertj-android ] : https://github.com/square/assertj-android
610
- [ system-current-time-millis-documentation ] : https://developer.android.com/reference/java/lang/System#currentTimeMillis()
611
485
[ xml-resource-parser-impl-javadoc ] : javadoc/latest/org/robolectric/android/XmlResourceParserImpl.html
0 commit comments