android – 检查Espresso是否启动了新的Activity

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

这是我的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("[email protected]"));
        onView(withId(R.id.login_password)).perform(typeText("11111111"));
        onView(withId(R.id.email_sign_in_button)).perform(click());

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

    }
}

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

意图(hasComponent(MainActivity.classenter code here.getName()));

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

这是完整的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.test.espresso.intent.Intents.internalIntended(org.hamcrest.Matcher,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)

解决方法

那么解决方案是检查这种方式:
intended(hasComponent(new ComponentName(getTargetContext(),MainActivity.class)));

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...