且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用改造和 rxjava 对 android 应用程序进行单元测试

更新时间:2023-01-06 07:56:46

如何测试 RxJava 和 Retrofit

1.摆脱静态调用——使用依赖注入

您的代码中的第一个问题是您使用静态方法.这不是一个可测试的架构,至少不容易,因为它使模拟实现变得更加困难.为了正确地做事,不要使用访问 ApiClient.getService()Api,而是通过构造函数将此 service 注入到 Presenter:

How to test RxJava and Retrofit

1. Get rid of the static call - use dependency injection

The first problem in your code is that you use static methods. This is not a testable architecture, at least not easily, because it makes it harder to mock the implementation. To do things properly, instead of using Api that accesses ApiClient.getService(), inject this service to the presenter through the constructor:

public class SplashPresenterImpl implements SplashPresenter {

private SplashView splashView;
private final ApiService service;

public SplashPresenterImpl(SplashView splashView, ApiService service) {
    this.splashView = splashView;
    this.apiService = service;
}

2.创建测试类

实现您的 JUnit 测试类并在 @Before 方法中使用模拟依赖项初始化演示者:

2. Create the test class

Implement your JUnit test class and initialize the presenter with mock dependencies in the @Before method:

public class SplashPresenterImplTest {

@Mock
ApiService apiService;

@Mock
SplashView splashView;

private SplashPresenter splashPresenter;

@Before
public void setUp() throws Exception {
    this.splashPresenter = new SplashPresenter(splashView, apiService);
}

3.模拟和测试

然后是实际的模拟和测试,例如:

3. Mock and test

Then comes the actual mocking and testing, for example:

@Test
public void testEmptyListResponse() throws Exception {
    // given
    when(apiService.syncGenres()).thenReturn(Observable.just(Collections.emptyList());
    // when
    splashPresenter.syncGenres();
    // then
    verify(... // for example:, verify call to splashView.navigateToHome()
}

这样你就可以测试你的 Observable + Subscription,如果你想测试 Observable 的行为是否正确,用 TestSubscriber 的实例订阅它.

That way you can test your Observable + Subscription, if you want to test if the Observable behaves correctly, subscribe to it with an instance of TestSubscriber.

使用 RxJava 和 RxAndroid 调度程序进行测试时,例如 Schedulers.io()AndroidSchedulers.mainThread(),您可能会在运行 observable/subscription 测试时遇到一些问题.

When testing with RxJava and RxAndroid schedulers, such as Schedulers.io() and AndroidSchedulers.mainThread() you might encounter several problems with running your observable/subscription tests.

第一个是 NullPointerException 在应用给定调度程序的行上抛出,例如:

The first is NullPointerException thrown on the line that applies given scheduler, for example:

.observeOn(AndroidSchedulers.mainThread()) // throws NPE

原因是 AndroidSchedulers.mainThread() 内部是一个 LooperScheduler,它使用了 android 的 Looper 线程.这种依赖在 JUnit 测试环境中不可用,因此调用会导致 NullPointerException.

The cause is that AndroidSchedulers.mainThread() is internally a LooperScheduler that uses android's Looper thread. This dependency is not available on JUnit test environment, and thus the call results in a NullPointerException.

第二个问题是,如果应用的调度器使用单独的工作线程来执行observable,那么执行@Test方法的线程和该工作线程之间就会出现竞争条件.通常它会导致在可观察执行完成之前返回测试方法.

The second problem is that if applied scheduler uses a separate worker thread to execute observable, the race condition occurs between the thread that executes the @Test method and the said worker thread. Usually it results in test method returning before observable execution finishes.

上述两个问题都可以通过提供符合测试标准的调度程序轻松解决,而且选择很少:

Both of the said problems can be easily solved by supplying test-compliant schedulers, and there are few options:

  1. 使用 RxJavaHooksRxAndroidPlugins API 覆盖对 Schedulers.?AndroidSchedulers.?的任何调用>,强制 Observable 使用,例如 Scheduler.immediate():

  1. Use RxJavaHooks and RxAndroidPlugins API to override any call to Schedulers.? and AndroidSchedulers.?, forcing the Observable to use, for example, Scheduler.immediate():

@Before
public void setUp() throws Exception {
        // Override RxJava schedulers
        RxJavaHooks.setOnIOScheduler(new Func1<Scheduler, Scheduler>() {
            @Override
            public Scheduler call(Scheduler scheduler) {
                return Schedulers.immediate();
            }
        });

        RxJavaHooks.setOnComputationScheduler(new Func1<Scheduler, Scheduler>() {
            @Override
            public Scheduler call(Scheduler scheduler) {
                return Schedulers.immediate();
            }
        });

        RxJavaHooks.setOnNewThreadScheduler(new Func1<Scheduler, Scheduler>() {
            @Override
            public Scheduler call(Scheduler scheduler) {
                return Schedulers.immediate();
            }
        });

        // Override RxAndroid schedulers
        final RxAndroidPlugins rxAndroidPlugins = RxAndroidPlugins.getInstance();
        rxAndroidPlugins.registerSchedulersHook(new RxAndroidSchedulersHook() {
            @Override
            public Scheduler getMainThreadScheduler() {
                return Schedulers.immediate();
        }
    });
}

@After
public void tearDown() throws Exception {
    RxJavaHooks.reset();
    RxAndroidPlugins.getInstance().reset();
}

这段代码要封装Observable测试,所以可以在@Before@After内完成如图,可以放入JUnit @规则 或放置在代码中的任何位置.只是不要忘记重置挂钩.

This code has to wrap the Observable test, so it can be done within @Before and @After as shown, it can be put into JUnit @Rule or placed anywhere in the code. Just don't forget to reset the hooks.

第二个选项是通过依赖注入向类(Presenters、DAO)提供显式的 Scheduler 实例,然后再次使用 Scheduler.immediate() (或其他适合测试的).

Second option is to provide explicit Scheduler instances to classes (Presenters, DAOs) through dependency injection, and again just use Schedulers.immediate() (or other suitable for testing).

正如@aleien 所指出的,您还可以使用注入的 RxTransformer 实例来执行 Scheduler 应用程序.

As pointed out by @aleien, you can also use an injected RxTransformer instance that executes Scheduler application.

我在生产中使用了第一种方法,效果很好.

I've used the first method with good results in production.