zoukankan      html  css  js  c++  java
  • Customizing the Test Runner

    There are several situations where you want to customize Robolectric's test runner to perform some operation before all tests are run, or even before each test method is run. One good example is initializing a dependency injection framework with a different set of dependencies for your test. Fortunately, Robolectric has a way to hook into the test lifecycle. If you define an Application class in your AndroidManifest.xml, Robolectric will automatically try and load a test version of your application class first. For example:

    Let's say you've defined a FooApplication in your manifest:

    <application android:name=".FooApplication">
    

    If you're using RoboGuice, you would initialize the injector in your Application class:

    public class FooApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
    
            ApplicationModule module = new ApplicationModule();
            setBaseApplicationInjector(this, DEFAULT_STAGE, newDefaultRoboModule(this), module);
        }
    }
    

    You can define a test version of the application named TestFooApplication:

    public class TestFooApplication extends FooApplication implements TestLifecycleApplication {
        @Override
        public void onCreate() {
            super.onCreate();
    
            TestApplicationModule module = new TestApplicationModule();
            setBaseApplicationInjector(this, DEFAULT_STAGE, newDefaultRoboModule(this), module);
        }
    
        @Override
        public void beforeTest(Method method) {
        }
    
        @Override
        public void prepareTest(Object test) {
            getInjector(this).injectMembers(test);
        }
    
        @Override
        public void afterTest(Method method) {
        }
    }
    

    Robolectric will load the test version of the application which you can use to load a different set of bindings during tests.

    ----------- Do not start just casually, and do not end just casually. -----------
  • 相关阅读:
    关于键保留表的一些汇总
    pl/sql,Oracle数据库中,不小心delete数据并提交后如何恢复被删数据
    创建视图的with check option选项。
    oracle中update,insert,delete的高级用法
    物化视图详解--介绍、创建方法、例子
    Oracle 视图备忘
    利用替换变量提高Oracle交互性(define)
    Sqlplus中的VARIABLE以及使用VARIABLE之后使用execute。
    Unity3d 引擎原理详细介绍
    Unity 3D主要特性和缺陷
  • 原文地址:https://www.cnblogs.com/yexiant/p/5692850.html
Copyright © 2011-2022 走看看