The intention is: the app starts with a welcome page(corresponding WelcomeActivity) which will last for 2 seconds, then will auto jump to main page(corresponding MainActivity). I want to write a test to cover that but failed...
My test code as below:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class WelcomeActivityTest {
@Test
public void should_navigate_to_main_activity() throws Exception {
Robolectric.setupActivity(WelcomeActivity.class);
ShadowApplication instance = ShadowApplication.getInstance();
Intent nextStartedActivity = instance.getNextStartedActivity();
assertNotNull(nextStartedActivity);
String className = nextStartedActivity.getComponent().getClassName();
assertThat(className, is(LoginActivity.class.getName()));
}
}
My implementation code as below:
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade, R.anim.hold);
WelcomeActivity.this.finish();
}
}, 2000);
}
}