gpt4 book ai didi

android - 检查是否使用 Espresso 启动了新 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:29 25 4
gpt4 key购买 nike

如果在我登录后启动了一个新 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

这是我的测试类:

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

}
}

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

intended(hasComponent(MainActivity.class在此处输入代码.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)

最佳答案

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

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

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

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

关于android - 检查是否使用 Espresso 启动了新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41890943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com