单元测试 - 验证Observable已订阅

时间:2016-11-09 04:47:57

标签: java unit-testing mockito rx-java observable

我有像这样的java代码

 mDataManager.getObservable("hello").subscribe( subscriber );

我希望verify以下Observable为.subscribe()

我试图模仿getObservable()verify

 Observable<Response> res = mock(Observable.class);
 when(mDataManager.getObservable("hello")).thenReturn(res);
 verify(res).subscribe();

但是有一个错误

Caused by: java.lang.IllegalStateException: onSubscribe function can not be null.
at rx.Observable.subscribe(Observable.java:8167)
at rx.Observable.subscribe(Observable.java:8158)
at rx.Observable.subscribe(Observable.java:7962)
....
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: omni.neo.hk.omniapiservice.v4.model.external.UserLoginBean.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:109)
at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:187)
at rx.internal.operators.OperatorDoOnEach$1.onNext(OperatorDoOnEach.java:82)
... 48 more

我认为这里不可能有mock一个Observable,但如果没有一个模拟的Observable,我就无法做verify(res).subscribe()

在这种情况下有任何建议吗?

4 个答案:

答案 0 :(得分:2)

我发现RxJava提供了一个名为TestSubject

的类

您可以像这样创建

private TestScheduler eventsScheduler = new TestScheduler();
private TestSubject<MyEvent> eventObservable = TestSubject.create(eventsScheduler);

这将为您提供返回布尔值的方法hasObservers()

@Test
public void testSubscription(){
    myTestClass.init(eventObservable);

    assertTrue(eventObservable.hasObservers());
}

TestSubject还允许您完美地发送事件的时间。

eventObservable.onNext(new MyEvent());
eventsScheduler.triggerActions(); 

答案 1 :(得分:1)

也许您可以将Observable.onSubscribe方法与RunTestOnContext规则一起使用? TestContext可以为您提供Async对象,只有在完成后才会终止测试。我认为,如果将其与Observable#doOnSubscribe结合使用,则可以实现所需的行为。

但是,有时使用Async可能会有点混乱。在下面的示例中,如果从未订阅observable,则永远不会评估doOnSubscribe函数,并且您的测试不会终止。

示例:

@RunWith(VertxUnitRunner.class)
public class SubscriptionTest {

  @Rule
  public RunTestOnContext vertxRule = new RunTestOnContext();

  @Test
  public void observableMustBeSubscribed(final TestContext context) {
    final Async async = context.async();
    final Observable<String> observable = Observable.just("hello").doOnSubscribe(async::complete);
    final Manager mock = mock(Manager.class);
    when(mock.getObservable()).thenReturn(observable);

    mock.getObservable().subscribe();
  }

  interface Manager {
    Observable<String> getObservable();
  }
}

答案 2 :(得分:0)

我不确定我是在回答问题,但我所以......

可以创建单元测试,以检查是否已对可观察对象进行了预订。请注意,在RxJava 1.x中,TestSubject支持此功能,但Subject实现可以促进该功能。 TestSubject是dropped for RxJava 2

不要试图像原始海报那样试图模仿observable,而是使用PublishSubject,运行业务逻辑,然后使用发布主题来查看是否正在观察Observable。下面是一些验证此测试的测试代码。我运行时测试通过。

有关代码的说明,请参阅代码注释。就个人而言,这个测试似乎是其他测试的冗余,可以验证控制器如何处理数据。如果控制器从未订阅并从数据服务接收数据,那么其他测试将失败。另一方面,测试取消订阅有助于避免内存泄漏,这可能是很高的价值。

import org.junit.Test;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.PublishSubject;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

/**
 * Tests whether it is possible to test whether a controller properly unsubscribes from an Observable.
 */
public class SubscribeUnsubscribeTests {

    /**
     * A service that returns a stream of strings.
     */
    interface StringService {
        Observable<String> getStrings();
    }

    /**
     * A controller that requests strings from the {@link StringService} and logs them
     * to system out as they are received.
     */
    class StringLoggingController {

        private StringService stringService;

        private Disposable stringSubscriptionDisposable;

        public StringLoggingController(StringService stringService) {
            this.stringService = stringService;
        }

        /**
         * Causese the controller to begin strings request and logging.
         */
        public void start() {
            stringSubscriptionDisposable = stringService.getStrings()
                    .subscribe(string -> System.out.print(string));
        }

        public void stop() {
            if (stringSubscriptionDisposable != null) {
                if (!stringSubscriptionDisposable.isDisposed()) {
                    stringSubscriptionDisposable.dispose();
                    stringSubscriptionDisposable = null;
                }
            }
        }
    }

    /**
     * A {@link StringService} that can report whether {@link StringService#getStrings()}
     * has observers.
     */
    class ReportIsSubscribedStringService implements StringService {

        private PublishSubject<String> publishSubject = PublishSubject.create();

        public Observable<String> getStrings() {
            return publishSubject;
        }

        /**
         * @return true if the {@link #getStrings()} observable has observers.
         */
        public boolean stringsAreBeingObserved() {
            return publishSubject.hasObservers();
        }
    }

    /**
     * Verifies that the {@link StringLoggingController} is subscribing to the service.
     */
    @Test
    public void stringsLoggingControllerSubscribesToStringService() {
        ReportIsSubscribedStringService service = new ReportIsSubscribedStringService();
        StringLoggingController controller = new StringLoggingController(service);

        controller.start();
        assertTrue(service.stringsAreBeingObserved());
    }

    /**
     * Verifies that the {@link StringLoggingController} is unsubscribing from the service.
     */
    @Test
    public void stringsLoggingControllerUnsubscribesFromStringService() {
        ReportIsSubscribedStringService service = new ReportIsSubscribedStringService();
        StringLoggingController controller = new StringLoggingController(service);

        controller.start();
        controller.stop();

        assertFalse(service.stringsAreBeingObserved());
    }
}

- 编辑 - 我之前写过,我无法测试取消订阅。我确实弄清楚如何测试。事实证明我的测试失败了,因为测试的代码没有正确取消订阅(哈哈 - 测试工作,去图)。我已经更新了上面的代码,以说明对订阅和取消订阅的测试。

答案 3 :(得分:0)

我今天花了几个小时意识到这是一个愚蠢的错误。 如果您使用的是PowerMockito,请检查您的@PrepareForTest。

@PrepareForTest({Observable.class})

也不要忘记嘲笑它。我是在@before:

做的
PowerMockito.mockStatic(Observable.class);

我希望它可以提供帮助。谢谢