O teste do Android Espresso Intents falha aleatoriamente com `` init () deve ser chamado antes de usar este método``
Atualmente, estou trabalhando em um projeto para testes de café expresso. Li vários documentos e sigo as práticas fornecidas para começar.
Tudo funciona bem. No entanto, quando se trata do teste relacionado ao Intents, o resultado é estranho.
A maior parte do tempo, os testes passaram no meu Mac, mas falharam no Windows do meu colega (nem todos os testes falham) com a mensagem de falhajava.lang.IllegalStateException: init() must be called prior to using this method
.
Estranhamente, se executarmos o teste de depuração no Android Studio transmitir o código passo a passo, ele passa.
aqui está o código de teste:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
@Rule public IntentsTestRule<MainActivity> mRule = new IntentsTestRule<>(MainActivity.class, true, false);
AccountManager accountManager;
MainActivity activity;
private void buildLoginStatus() throws AuthenticatorException {
DanteApp app = (DanteApp) InstrumentationRegistry.getTargetContext().getApplicationContext();
accountManager = app.getDanteAppComponent().accountManager();
DoctorModel doctorModel = AccountMocker.mockDoctorModel();
accountManager.save(doctorModel.doctor);
accountManager.setAccessToken(doctorModel.access_token, false);
}
@Before public void before() throws Exception {
buildLoginStatus();
// must login
assertThat(accountManager.hasAuthenticated(), is(true));
activity = mRule.launchActivity(null);
// block all of the outer intents
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
}
@After public void tearDown() throws Exception {
accountManager.delete();
}
// failed
@Test public void testViewDisplay() throws Exception {
// check tabhost is displayed
onView(withClassName(equalTo(TabHost.class.getName()))).check(matches(isDisplayed()));
// check toolbar is displayed
onView(withClassName(equalTo(ToolBar.class.getName()))).check(matches(isDisplayed()));
}
// passed
@Test public void testCallServiceHotline() throws Exception {
// switch to the account tab layout
onView(withChild(withText(R.string.account))).perform(click());
// click account menu to make a service call
onView(withId(R.id.contact)).perform(click());
// check call start expectly
intended(allOf(
not(isInternal()),
hasAction(Intent.ACTION_DIAL),
hasData(Uri.parse("tel:" + activity.getString(R.string.call_service)))
));
}
// failed
@Test public void testOpenSettingsUI() throws Exception {
// stub all internal intents
Intents.intending(isInternal())
.respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
onView(withChild(withText(R.string.account))).perform(click());
onView(withId(R.id.setting)).perform(click());
// check open settings activity successfully
intended(anyOf(
hasComponent(SettingActivity.class.getName())
));
}
}
A versão da biblioteca de testes (quase todas as dependências estão atualizadas e usamos dispositivos de física e emulador para testar):
regra: 0.4.1corredor: 0.4.1espresso- *: 2.2.1support- *: 23.1.0Qualquer idéia merece uma apreciação. Obrigado!