Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to get Context when testing with ActivityInstrumentationTestCase2?

I'm running a junit test in Android which extends ActivityInstrumentationTestCase2. I'm using this on order to start up an activity.

The activity uses a subclass of the application object to obtain some parameters. I get the application object from the context.

Unfortunately, ActivityInstrumentationTestCase2 does not provide access to the context. Is there a way to access the context before getting the activity?

like image 314
Jack BeNimble Avatar asked Dec 04 '22 08:12

Jack BeNimble


1 Answers

You can get the application context from the instrumentation object:

getInstrumentation().getTargetContext().getApplicationContext()
like image 75
Marco RS Avatar answered Jan 03 '23 10:01

Marco RS