在android测试上重启应用程序

时间:2015-12-18 18:59:15

标签: 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();
    }
}

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");
        }
    }
}

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

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

ApplicationTest:

  public class ApplicationTest extends ApplicationTestCase<SampleApplication> {
        public ApplicationTest() {
            super(SampleApplication.class);
        }
    }

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  
    }
}

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

1 个答案:

答案 0 :(得分:1)

我相信,您真正遇到的问题是InstrumentationTestRunner问题:How to prevent ActivityUnitTestCase from calling Application.onCreate?(显然,没有明显的解决方法)

即。 TestRunner无论如何都会在初始化期间致电onCreate(),因此,当您致电createApplication()后,您的Sdk已经初始化。

关于问题本身 - 我相信,唯一的选择是重新考虑Sdk类的架构(或添加一些&#34;重置&#34;函数等)

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();
    }
}

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");
        }
    }
}

试验:

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();
    }
}

NB!如果你为androidTest应用特殊的AndroidManifest,你可以减少一点点。然后,当测试开始之前它在调用onCreate()时,你不会对TestRunner的问题感到挣扎。

我希望,这有帮助