检查Espresso是否启动了新活动

时间:2017-01-27 09:44:38

标签: android android-espresso

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

dropcols.py

这是我的测试类:

dropcols.pyw

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

意图(hasComponent(MainActivity.class 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 的getName())。);

我该如何解决这个问题?我做错了什么?

这是完整的堆栈跟踪:

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()));

    }
}

2 个答案:

答案 0 :(得分:17)

Lefteris&#39;解决方案或此代码有效:

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

但是不要忘记将ActivityTestRule更改为IntentsTestRule

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

答案 1 :(得分:3)

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

    intended(hasComponent(new ComponentName(getTargetContext(), MainActivity.class)));