Reputation: 147
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.
Upvotes: 5
Views: 4608
Reputation: 1021
This happens probably because Android is trying to initialize this activity twice. You either use ActivityTestRule
to launch the activity and manage by itself, or do it yourself with ActivityScenario.launch
. I suggest using ActivityTestRule
in your case.
doc: https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule
Upvotes: 0
Reputation: 23
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.
Upvotes: 0
Reputation: 31
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.
Upvotes: 3