检查Espresso是否启动了新活动

rhe*_*0b1 9 android android-espresso

如果在我的登录后推出了一个新的活动,那么我知道一切都正常.我试图实现这个,但我现在得到了

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.test.espresso.intent.Intents.internalIntended(org.hamcrest.Matcher, android.support.test.espresso.intent.VerificationMode, java.util.List)' on a null object reference
Run Code Online (Sandbox Code Playgroud)

这是我的Test类:

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> mLoginActivityActivityTestRule =
            new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void clickLoginButton_ShowsSnackBarRightCredentials() throws Exception {

        onView(withId(R.id.login_email)).perform(typeText("a@aa.aa"));
        onView(withId(R.id.login_password)).perform(typeText("11111111"));
        onView(withId(R.id.email_sign_in_button)).perform(click());

        intended(hasComponent(MainActivity.class.getName()));

    }
}
Run Code Online (Sandbox Code Playgroud)

我在之前的问题中发现这行代码应该对我有所帮助,但是这一行产生了NullPointer.

意图(hasComponent(MainActivity.class enter code here.getName()));

我怎样才能解决这个问题?我究竟做错了什么?

这是完整的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.test.espresso.intent.Intents.internalIntended(org.hamcrest.Matcher, android.support.test.espresso.intent.VerificationMode, java.util.List)' on a null object reference
at android.support.test.espresso.intent.Intents$2.check(Intents.java:190)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Run Code Online (Sandbox Code Playgroud)

Kar*_*lin 20

Lefteris的解决方案或此代码有效:

intended(hasComponent(MainActivity.class.getName()));
Run Code Online (Sandbox Code Playgroud)

但是不要忘记更改ActivityTestRuleIntentsTestRule了.

 @Rule
 public IntentsTestRule<LoginActivity> mLoginActivityActivityTestRule =
            new IntentsTestRule<>(LoginActivity.class);
Run Code Online (Sandbox Code Playgroud)

  • 如何在使用`intended(hasComponent(MainActivity.class.getName()));`之前在前台准备好MainActivity?目前我正在使用`Thread.sleep(2000L);` 在按钮点击之后和调用之前,但我想知道,也许还有其他更好的方法。 (2认同)

Lef*_*ris 6

那么解决方案是这样检查:

intended(hasComponent(OnboardingActivity::class.java.name))
Run Code Online (Sandbox Code Playgroud)

为了执行此检查,您需要添加意图库(androidTestImplementation "androidx.test.espresso:espresso-intents:$intentsVersion") 并通过调用来初始化它:

Intents.init()

  • 对我来说这不起作用,应用程序进入 Mainactivity,但测试结果超时。 (2认同)