android - 在android测试中重新启动应用程序

标签 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 测试

应用测试:

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

安卓测试样本:

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测试的应用程序吗? 我在这里做错了什么吗?

最佳答案

我相信,您真正遇到的问题是 InstrumentationTestRunner的问题:How to prevent ActivityUnitTestCase from calling 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();
    }
}

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

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

希望对你有帮助

关于android - 在android测试中重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34362262/

相关文章:

java - 图像处理和比较Android应用程序

java - 在不分配内存的情况下将整数转换为字符串

mysql - 包括数据库在内的单元测试应用程序太慢

unit-testing - Grails:在另一个服务中模拟一个服务及其方法

Android Kotlin (Instrumented) 测试,断言 ImageButton 具有正确的资源不起作用

Android 4.3 BLE Characteristic 怎么写

android - Google 云端硬盘客户端/服务器应用程序

python-3.x - Python 测试对实例化为另一个类中的变量的类方法的调用

安卓 : Separate resources for 'androidTest' '

android - SourceCodeScanner 不调用 visitMethodCall