Why is activity not launching in Android Instrumentation test (Activity Test) with espresso?

Viewed 2073

My test class:

@RunWith(AndroidJUnit4.class)
    public class MainActivityTest {
    
        @Rule
        public ActivityScenarioRule<MainActivity> rule = new  ActivityScenarioRule<>(MainActivity.class);
        ActivityScenario<MainActivity> scenario;
    
        Context context = ApplicationProvider.getApplicationContext();
    
        @Before
        public void launchActivity() {
            scenario = ActivityScenario.launch(MainActivity.class);
            scenario.moveToState(Lifecycle.State.CREATED);
        }
    
        @Test
        public void sendLocationButton_clicked_activateLongClickToastAppear() {
            onView(withId(R.id.location_button)).perform(click());
            onView(withText("Long press it")).inRoot(withDecorView(not(is(getActivity(context).getWindow().getDecorView())))).check(matches(isDisplayed()));
        }
        
    
        @After
        public void tearDown() throws Exception {
            scenario.close();
        }
    }

Error:

java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, RESUMED, CREATED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE")
    at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)
    at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272)
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$0$ActivityScenarioRule(ActivityScenarioRule.java:70)
    at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$0.get(Unknown Source:2)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

This is just a part of the error, as stack overflow reject such a huge code in question.

I just want to test if a Toast is appeared or not on a button click. No idea of what is happening. Please help. Thanks in advance.

3 Answers

It could be the same issue I ran into recently.

Try adding

debugImplementation "androidx.test:core:1.3.0"

to your gradle.build file at the app level, with the core version you are using.

Also, you do not need to use ActivityScenarioRule and ActivityScenario together. Pick the one that suits your needs.

ActivityScenarioRule is similar to ActivityTestRule. It will simply launch the activity before the test (it internally calls ActivityScenario to launch the activity).

ActivityScenario gives you more control, as it allows you to launch an activity during a test or even switch activities during a test.

With ActivityScenarioRule you don't need to cast launch as before since the rule already casts it for you. Also cast the close after the test is run. Just remove the before and after rule and the test should work fine. If you need with this library, get some information from the activity. You can create a Context instance with ApplicationProvider.getApplicationContext and then you can use it as it was on onActivity before.

Related