Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Android Activity with different Application class

I´m looking for a way to test an Activity with JUnit4 and the ActivityTestRule, but with a different application class (e.g. mocked or inherited). I was able to get this for library projects using the manifest merge and tools:replace="android:name" on the application tag in the androidTest/AndroidManifest.xml. This however does not work for applications.

Any ideas how this can be done?

like image 903
Fabian Avatar asked Nov 27 '15 14:11

Fabian


Video Answer


1 Answers

You can subclass the AndroidJUnitRunner with your own runner and override newApplication() with your custom application class.

public class CustomTestRunner extends AndroidJUnitRunner {

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

Make sure to update your build.gradle with the new runner like this:

testInstrumentationRunner "com.mypackage.name.path.CustomTestRunner"

like image 131
Be_Negative Avatar answered Oct 10 '22 03:10

Be_Negative