|
| 1 | +--- |
| 2 | +title: FlutterMain.setIsRunningInRobolectricTest on Android removed |
| 3 | +description: The test-only FlutterMain.setIsRunningInRobolectricTest API on the Android engine is consolidated into the FlutterInjector. |
| 4 | +--- |
| 5 | + |
| 6 | +## Summary |
| 7 | + |
| 8 | +If you write Java JUnit tests (such as Robolectric tests) against the Flutter |
| 9 | +engine's Java embedding and used the |
| 10 | +`FlutterMain.setIsRunningInRobolectricTest(true)` API, replace it with |
| 11 | + |
| 12 | +```java |
| 13 | +FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); |
| 14 | +FlutterInjector.setInstance( |
| 15 | + new FlutterInjector.Builder() |
| 16 | + .setFlutterLoader(new FlutterLoader(mockFlutterJNI)) |
| 17 | + .build()); |
| 18 | +``` |
| 19 | + |
| 20 | +This should be very uncommon. |
| 21 | + |
| 22 | +## Context |
| 23 | + |
| 24 | +The `FlutterMain` class itself is being deprecated and replaced with the |
| 25 | +`FlutterInjector` class. The `FlutterMain` class uses a number of |
| 26 | +static variables and functions than make it difficult to test. |
| 27 | +`FlutterMain.setIsRunningInRobolectricTest()` is one ad-hoc static |
| 28 | +mechanism to allow tests to run on the host machine on JVM without |
| 29 | +loading the libflutter.so native library (which can't be done on the host |
| 30 | +machine). |
| 31 | + |
| 32 | +Rather than one-off solutions, all dependency injections needed for tests |
| 33 | +in Flutter's Android/Java engine embedding are now moved to the |
| 34 | +[`FlutterInjector`](https://cs.opensource.google/flutter/engine/+/master:shell/platform/android/io/flutter/FlutterInjector.java) |
| 35 | +class. |
| 36 | + |
| 37 | +Within the `FlutterInjector` class, the `setFlutterLoader()` Builder |
| 38 | +function allows for control of how the [`FlutterLoader`](https://cs.opensource.google/flutter/engine/+/master:shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java) |
| 39 | +class locates and loads the libflutter.so library. |
| 40 | + |
| 41 | +## Description of change |
| 42 | + |
| 43 | +https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-599e1d64442183ead768757cca6805c3L154 |
| 44 | +removes the `FlutterMain.setIsRunningInRobolectricTest()` testing function. |
| 45 | + |
| 46 | +https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-f928557f2d60773a8435366400fa42ed |
| 47 | +adds a `FlutterInjector` class to assist testing. |
| 48 | + |
| 49 | +https://github.com/flutter/engine/pull/20473 further refactors FlutterLoader and FlutterJNI |
| 50 | +to allow for additional mocking/testing. |
| 51 | + |
| 52 | +## Migration guide |
| 53 | + |
| 54 | +Code before migration: |
| 55 | + |
| 56 | +```java |
| 57 | +FlutterMain.setIsRunningInRobolectricTest(true); |
| 58 | +``` |
| 59 | + |
| 60 | +Code after migration: |
| 61 | + |
| 62 | +```java |
| 63 | +FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); |
| 64 | +FlutterInjector.setInstance( |
| 65 | + new FlutterInjector.Builder() |
| 66 | + .setFlutterLoader(new FlutterLoader(mockFlutterJNI)) |
| 67 | + .build()); |
| 68 | +``` |
| 69 | + |
| 70 | +## Timeline |
| 71 | + |
| 72 | +Landed in version: 1.22.0-2.0.pre.133<br> |
| 73 | +In stable release: not yet |
0 commit comments