Test Android Activity with different Application class

大憨熊 提交于 2019-12-18 15:28:11

问题


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?


回答1:


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"



来源:https://stackoverflow.com/questions/33959061/test-android-activity-with-different-application-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!