Using a custom application with InstrumentationTestCase

I have an ActivityInstrumentationTestCase2 (which is a subclass of InstrumentationTestCase). When starting my test boxes, I need to start my actions using the special TestApplication object, because TestApplication objects have a specific configuration for these tests.

However, I see no way to configure ActivityInstrumentationTestCase2 test testers to use the test application object. Is there any way to do this?

+5
source share
2 answers

I don't know if there is a better way, but I was able to accomplish this using my own TestRunner.

public class MyInstrumentationTestRunner extends InstrumentationTestRunner {

    @Override
    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return new MyTestApplication(context);       
    }


}

AndroidManifest.xml, :

<instrumentation android:name="com.mypackage.test.MyInstrumentationTestRunner" ... />

IDE, . , - :

adb shell am instrument -w com.mypackage/com.mypackage.test.MyInstrumentationTestRunner
+5

@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    return Instrumentation.newApplication(YourAppClass.class, context);      
}

+4

All Articles