FlutterMain.setIsRunningInRobolectricTest on Android removed
Summary
If you write Java JUnit tests (such as Robolectric tests)
against the Flutter engine’s Java embedding and used the
FlutterMain.setIsRunningInRobolectricTest(true)
API,
replace it with the following:
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
FlutterInjector.setInstance(
new FlutterInjector.Builder()
.setFlutterLoader(new FlutterLoader(mockFlutterJNI))
.build());
This should be very uncommon.
Context
The FlutterMain
class itself is being deprecated and replaced with the
FlutterInjector
class. The FlutterMain
class uses a number of
static variables and functions than make it difficult to test.
FlutterMain.setIsRunningInRobolectricTest()
is one ad-hoc static
mechanism to allow tests to run on the host machine on JVM without
loading the libflutter.so
native library
(which can’t be done on the host machine).
Rather than one-off solutions, all dependency injections needed for tests
in Flutter’s Android/Java engine embedding are now moved to the
FlutterInjector
class.
Within the FlutterInjector
class,
the setFlutterLoader()
Builder
function allows for control of how the
FlutterLoader
class locates and loads
the libflutter.so
library.
Description of change
This engine commit removed the
FlutterMain.setIsRunningInRobolectricTest()
testing function;
and the following commit added a
FlutterInjector
class to assist testing.
PR 20473 further refactored FlutterLoader
and FlutterJNI
to allow for additional mocking and testing.
to allow for additional mocking/testing.
Migration guide
Code before migration:
FlutterMain.setIsRunningInRobolectricTest(true);
Code after migration:
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
FlutterInjector.setInstance(
new FlutterInjector.Builder()
.setFlutterLoader(new FlutterLoader(mockFlutterJNI))
.build());
Timeline
Landed in version: 1.22.0-2.0.pre.133
In stable release: 2.0.0