Get context of test project in Android junit test case

后端 未结 10 2225
闹比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: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));
        }
    }
    

提交回复
热议问题