Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a new Activity is started with Espresso

If a new Activity is lauched after my Login then I know that everything went right. I tried to implement this but I got now an

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

This is my Test class:

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

    }
}

I found in previous questions that this line of code should help me but this line produces the NullPointer.

intended(hasComponent(MainActivity.classenter code here.getName()));

How can I solve this probelm? What am I doing wrong?

This is the full stack trace:

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)
like image 215
botscripter Avatar asked Jan 27 '17 09:01

botscripter


People also ask

How do you test espresso intent?

Validate intents Using the intended() method, which is similar to Mockito. verify() , you can assert that a given intent has been seen. However, Espresso-Intents doesn't stub out responses to intents unless you explicitly configure it to do so. // User action that results in an external "phone" activity being launched.

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.


1 Answers

Lefteris' solution or this code works:

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

But don't forget to change ActivityTestRule to IntentsTestRule too.

 @Rule
 public IntentsTestRule<LoginActivity> mLoginActivityActivityTestRule =
            new IntentsTestRule<>(LoginActivity.class);
like image 92
Karl Jamoralin Avatar answered Oct 02 '22 15:10

Karl Jamoralin