Get context of test project in Android junit test case

后端 未结 10 2188
闹比i
闹比i 2020-12-02 12:58

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

Note: The test is NOT instru

相关标签:
10条回答
  • 2020-12-02 13:13

    If you want to get the context with Kotlin and Mockito, you can do it in the following way:

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

    I Hope its help you

    0 讨论(0)
  • 2020-12-02 13:15

    @RunWith(AndroidJUnit4.class) let you use Android Context

    /**
     * Instrumented test, which will execute on an Android device.
     *
     * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
     */
    @RunWith(AndroidJUnit4.class)
    public class ExampleInstrumentedTest {
        @Test
        public void useAppContext() {
            // Context of the app under test.
            Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
            assertEquals("com.android.systemui", appContext.getPackageName());
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-02 13:16

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

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

    You can bypass the @hide annotation using reflection.

    Just add the following method in your 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() any time you want. :)

    0 讨论(0)
  • 2020-12-02 13:20

    After some research the only working solution seems to be the one yorkw pointed out already. You'd have to extend InstrumentationTestCase and then you can access your test application's context using getInstrumentation().getContext() - here is a brief code snippet using the above suggestions:

    public class PrintoutPullParserTest extends InstrumentationTestCase {
    
        public void testParsing() throws Exception {
            PrintoutPullParser parser = new PrintoutPullParser();
            parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration));
        }
    }
    
    0 讨论(0)
  • 2020-12-02 13:23

    There's new approach with Android Testing Support Library (currently androidx.test:runner:1.1.1). Kotlin updated example:

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

    If you want also app context run:

    InstrumentationRegistry.getInstrumentation().targetContext
    

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

    Look here: What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?

    0 讨论(0)
  • 2020-12-02 13:24
    import androidx.test.core.app.ApplicationProvider;
    
        private Context context = ApplicationProvider.getApplicationContext();
    
    0 讨论(0)
提交回复
热议问题