When using Hilt with Espresso I found that my activity is either not resumed or not destroyed. When I comment out the very first test that launches my activity the second one started to pass. The workaround is to separate each test however this will produce a lot of boiler plate code.
Also I have a custom activity inside the tests that inherits from my main activity.
@RunWith(AndroidJUnit4.class)
@HiltAndroidTest
public class MainActivityTest {
// ... initilisation & mocks
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
hiltAndroidRule.inject();
context = getInstrumentation().getTargetContext();
// ... other initialisation and setup
}
// Helper classes and functions
@AndroidEntryPoint
public class ModifiedMainActivity extends MainActivity {
// ... custom activity implementations
}
// Factory
private SingleActivityFactory<MainActivity> activityFactory = new SingleActivityFactory<MainActivity>(MainActivity.class) {
@Override
protected MainActivity create(Intent intent) {
ModifiedMainActivity mainActivity = new ModifiedMainActivity();
// other custom activity configuration & setup
return mainActivity;
}
}
@After
public void tearDown() {
// ... other code to tear down some stubs
}
}
Any ideas?
I can provide some code but other is under NDA.
My rules:
@Rule(order = 0)
public HiltAndroidRule hiltAndroidRule = new HiltAndroidRule(this);
@Rule(order = 1)
public ActivityTestRule<MainActivity> mainActivityRule = new ActivityTestRule<>(activityFactory, true, false);
Test example:
@Test
public void test_report_system_back_button_pressed_when_closing_main_activity() {
Intent intent = new Intent(Intent.ACTION_MAIN);
final MainActivity mainActivity = mainActivityRule.launchActivity(intent);
getInstrumentation().runOnMainSync(mainActivity::onBackPressed);
verify(mockedHelper, times(1)).reportEvent(eq(Codes.SYSTEM_BACK_PRESSED), eq("SystemBackPressed"), nullable(Map.class));
}
Exception:
java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:176)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:88)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:37)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:27)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:63)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:280)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:272)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Aucun commentaire:
Enregistrer un commentaire