Get test project context in Android junit test case

Does anyone know how you can get the context of a test project in a test example of Android junit (extends AndroidTestCase).

Note. The test is not a verification test.

Note 2: I need the context of the test project, not the context of the actual application under test.

I need this to load some files from assets from a test project.

+97
android junit
Dec 22 '11 at 2:55 a.m.
source share
10 answers

A new approach has appeared with the Android Testing Support Library (currently androidx.test:runner:1.1.1 ). Kotlin updated the example:

 class ExampleInstrumentedTest { lateinit var instrumentationContext: Context @Before fun setup() { instrumentationContext = InstrumentationRegistry.getInstrumentation().context } @Test fun someTest() { TODO() } } 

If you want to also launch the application context:

 InstrumentationRegistry.getInstrumentation().targetContext 

Full example: https://github.com/fada21/AndroidTestContextExample

Take a look here: what is the difference between getTargetContext () and getContext (in InstrumentationRegistry)?

+114
Jun 17 '15 at 11:52
source share

After some research, the only working solution seems to be already pointed out in one yorkw. You will need to extend InstrumentationTestCase, and then you can access the context of the test application using getInstrumentation (). GetContext () is a short piece of code that uses the above recommendations:

 public class PrintoutPullParserTest extends InstrumentationTestCase { public void testParsing() throws Exception { PrintoutPullParser parser = new PrintoutPullParser(); parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration)); } } 
+37
Jan 15 '12 at 14:17
source share

As you can read in the AndroidTestCase source code , the getTestContext() method is hidden.

 /** * @hide */ public Context getTestContext() { return mTestContext; } 

You can get around the @hide annotation with reflection.

Just add the following method to AndroidTestCase :

 /** * @return The {@link Context} of the test project. */ private Context getTestContext() { try { Method getTestContext = ServiceTestCase.class.getMethod("getTestContext"); return (Context) getTestContext.invoke(this); } catch (final Exception exception) { exception.printStackTrace(); return null; } } 

Then call getTestContext() at any time. :)

+25
Jan 09 '13 at 10:16
source share

Update: AndroidTestCase This class is deprecated at API level 24. Use InstrumentationRegistry instead. New tests should be written using the Android Testing Support Library. Link to ad

You must expand from AndroidTestCase instead of TestCase.

AndroidTestCase class overview
Extend this if you need access to resources or other things that depend on the context of activity.

AndroidTestCase - Android Developers

+4
Mar 06 '16 at 15:56
source share

If you want to get context with Kotlin and Mockito, you can do it like this:

 val context = mock(Context::class.java) 

Hope this helps you

+2
Sep 28 '18 at 16:01
source share

This is the right way to get context. Other methods are outdated

 import androidx.test.platform.app.InstrumentationRegistry InstrumentationRegistry.getInstrumentation().context 
+1
Mar 15 '19 at 13:12
source share

If you only need access to the resources of your project, you can use the getActivity () method of the ActivityInstrumentationTestCase2 class :

  //... private YourActivityClass mActivity; @Override protected void setUp() throws Exception { //... mActivity = getActivity(); } //... public void testAccessToResources() { String[] valueList; valueList = mActivity.getResources().getStringArray( com.yourporject.R.array.test_choices); } 
0
Aug 21 '14 at 1:47
source share

Other answers are out of date. Right now, when you extend AndroidTestCase, there is an mContext Context object that you can use.

0
Jun 12 '16 at 21:03
source share

You can use Robolectric for unit testing Android on the JVM.

Robolectric is an infrastructure that allows you to write unit tests and run them on the desktop JVM while still using the Android API.

Robolectric provides a JVM-compatible version of the android.jar file. Robolectric handles presentation inflation, resource loading, and much more, which is implemented in native C code on Android devices.

 dependencies { ... // Robolectric testCompile "org.robolectric:robolectric:3.3.2" } 
  1. Your tests should be stored in the src/test directory.
  2. The class containing your Robolectric test should be annotated with @RunWith(RobolectricTestRunner.class runner).
  3. It should also use @Config () to point to your BuildConfig.class class.

for example

 @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class) public class WelcomeActivityTest { private WelcomeActivity activity; @Before public void setUp() throws Exception { activity = Robolectric.buildActivity( WelcomeActivity.class ) .create() .resume() .get(); } @Test public void shouldNotBeNull() throws Exception { assertNotNull( activity ); } } 

Find out more here.

0
Jul 18 '19 at 20:31
source share
 import androidx.test.core.app.ApplicationProvider; private Context context = ApplicationProvider.getApplicationContext(); 
0
Aug 22 '19 at 13:17
source share



All Articles