android / android/android-test
Calling finish() after onStart() does not move its state to DESTROYED but remains in RESUMED
- Dominant language
- Java
- Stars
- 1.2k
- Forks
- 342
- Avg merge
- 11h 29m
- Merged PRs (30d)
- 2
Description
### Description
In Robolectric local unit test,
calling finish() in onStart() does not move its state to DESTROYED but remains in RESUMED.
This StackOverflow question (https://stackoverflow.com/questions/64525956/junit-framework-assertionfailederror-expecteddestroyed-but-wasresumed) may address the same issue.
### Steps to Reproduce
```
public class FinishOnCreateActivity extends AppCompatActivity {
@Override
public void onCreate() {
super.onCreate();
finish();
}
}
public class FinishOnStartActivity extends AppCompatActivity {
@Override
public void onStart() {
super.onStart();
finish();
}
}
@RunWith(AndroidJUnit4.class)
public class ActivityTest {
@Test
public void testFinishOnCreateActivity() {
try (ActivityScenario scenario =
ActivityScenario.launch(FinishOnCreateActivity.class)) {
assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
}
}
@Test
public void testFinishOnStartActivity() {
try (ActivityScenario scenario =
ActivityScenario.launch(FinishOnStartActivity.class)) {
assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
}
}
}
```
### Expected Results
Pass both tests
### Actual Results
Pass testFinishOnCreateActivity(),
Fail testFinishOnStartActivity().
### AndroidX Test and Android OS Versions
androidx.test 1.3.0
robolectric 4.4
### Link to a public git repo demonstrating the problem:
None
Contributor guide
Research direction
Start by reproducing the issue with the FinishOnCreateActivity and FinishOnStartActivity examples and the two ActivityScenario tests from the report. Compare the lifecycle state observed after finish() in onCreate() and onStart(); done means both tests report Lifecycle.State.DESTROYED.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- mobile, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100