在android测试上重启应用程序

iGo*_*oDa 7 android unit-testing android-testing

我正在创建一个库,它将根据用户默认设置处理信息,并将其保存在SharedPreferences上,开发人员可以在初始化我的库之前对其进行修改.

SDK应该只为每个应用程序实例初始化一次,否则会触发RuntimeError.所以在Application类的应用程序端应该是这样的:

public class SampleApplication extends Application {
    @Override
    public void onCreate() {
       super.onCreate();

       //Here I can do something that will change the default configs

       //Here I initialize the SDK singleton method
       Sdk.initialize();
    }
}
Run Code Online (Sandbox Code Playgroud)

sdk抽象实现:

public class Sdk {

    private static SampleApplication sInstance;

    private void Sdk(){
    }

    public static SampleApplication getInstance() throws RuntimeException {
        if (sInstance == null) {
            throw new RuntimeException();
        }
        return sInstance;
    }

    public static void initialize() {
        if (sInstance == null) {
            sInstance = new Sdk();
            //save some information according to what is on the default configurations
        } else {
            throw new RuntimeException("Method was already initialized");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我想测试几个方案来调用这个方法时(每个应用程序实例只能调用一次),就会出现问题.

所以我创建了一个扩展ApplicationTest的Android测试

ApplicationTest:

  public class ApplicationTest extends ApplicationTestCase<SampleApplication> {
        public ApplicationTest() {
            super(SampleApplication.class);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Android测试示例:

public class SampleTest extends ApplicationTest {

    @Override
    protected void setUp() throws Exception {
// Here I restart the user preferences although I need to restart the application
//        terminateApplication();
//        createApplication();
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testDefaultSettings() {
        // Here is where I want to restart application input
        // some values on the user preferences settings in order 
        // to test the output on sharedpreferences by the initialized method  
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图再次终止并创建应用程序,但没有成功.我的问题是有可能重新启动Android测试的应用程序吗?我在这里做错了吗?

Kon*_*nov 1

我相信,您真正遇到的问题是一个InstrumentationTestRunner问题:如何防止 ActivityUnitTestCase 调用 Application.onCreate?(而且,显然,没有明显的解决办法)

即 TestRunner 无论如何都会onCreate()在初始化期间调用,因此一旦您调用createApplication(),您Sdk就已经初始化了。

关于问题本身 - 我相信,唯一的选择是重新思考类的架构Sdk(或添加一些“重置”功能等)

public class TestApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Sdk.terminate(); - If you specify TestApplication as an 
        //                    application class in AndroidManifest, 
        //                    you'll have to uncomment this(due to issue with test runner)
        Sdk.initialize();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Sdk.terminate();
    }
}
Run Code Online (Sandbox Code Playgroud)

Sdk班级

public class Sdk {

    private static Sdk sInstance;
    private void Sdk(){
    }

    public static Sdk getInstance() throws RuntimeException {
        if (sInstance == null) {
            throw new RuntimeException();
        }
        return sInstance;
    }

    public static void terminate() {
        sInstance = null;
    }

    public static void initialize() {
        if (sInstance == null) {
            sInstance = new Sdk();
            //save some information according to what is on the default configurations
        } else {
            throw new RuntimeException("Method was already initialized");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

测试:

public class MyApplicationTest extends ApplicationTestCase<TestApplication> {

    public MyApplicationTest() {
        super(TestApplication.class);
    }

    public void testMultiplicationTests() {
        createApplication();

        int answer = 42;
        assertEquals(42, answer);

        terminateApplication();
    }


    public void testDefaultSettings() {
        createApplication();

        assertNotNull(Sdk.getInstance());

        terminateApplication();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意!如果你为 androidTest 应用特殊的 AndroidManifest,你可以让它变得不那么痛苦。然后,当 TestRunner 在测试开始之前自行调用 onCreate() 时,您将不会遇到 TestRunner 的问题。

我希望,它有帮助