Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Application in espresso?

I'm using espresso to add tests to my Android application.

In my TestActivity, I have some views that are only visible when the user is signed. The userIsSignedIn flag is stored in MyApp which is a subclass of android.app.Application. Is there a way I can access an instance of MyApp within the test case?

If not, what is alternative way to do this?

Thanks

like image 828
menawi Avatar asked Nov 16 '15 10:11

menawi


1 Answers

In your test class you can use something like this:

@Rule
public ActivityTestRule<MainActivity> mActivityRule =
        new ActivityTestRule<>(MainActivity.class);

Then in your test method:

@Test
public void doSomething() {

   //....
   mActivityRule.getActivity().getApplication();

}
like image 157
Gabriele Mariotti Avatar answered Oct 11 '22 12:10

Gabriele Mariotti