Test Android Activity with different Application class

前端 未结 1 1190
余生分开走
余生分开走 2020-12-15 07:23

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

相关标签:
1条回答
  • 2020-12-15 08:17

    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"

    0 讨论(0)
提交回复
热议问题