Skip to content

hyleung/rx-testkit

Repository files navigation

#RX TestKit: AssertJ for RxJava Build Status

This library provides a set of AssertJ assertions that can be used to make unit testing of RxJava code a little easier.

RxJava 2.x

via Maven:

<dependency>
    <groupId>com.github.hyleung</groupId>
    <artifactId>rx-testkit-java</artifactId>
    <version>2.0.0</version>
    <scope>test</scope>
</dependency>

via Gradle

test 'com.github.hyleung:rx-testkit-java:2.0.0'

RxJava 1.x

For RxJava 1.x, the latest release of this library is 1.1.0.

via Maven:

<dependency>
    <groupId>com.github.hyleung</groupId>
    <artifactId>rx-testkit-java</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

via Gradle

test 'com.github.hyleung:rx-testkit-java:1.1.0'

##Examples:

Assert that an Observable has completed…

Observable<String> observable = Observable.just("foo");

assertThat(observable)
    .hasCompleted();

…or hasn't completed:

Observable<String> observable = Observable.just("foo");

assertThat(observable)
    .hasNotCompleted();

…or has values (returns an AbstractListAssert)

Observable<String> observable = Observable.just("foo");

assertThat(observable)
    .values()
    .contains("foo");

…or emits a certain error (also returns an AbstractListAssert)

Exception myException = new Exception();

Observable<String> observable = Observable.error(myException);

assertThat(observable)
    .failures()
    .contains(myException);

...or mess around with time using a TestScheduler:

 TestScheduler scheduler = new TestScheduler();
 Observable<Integer> observable = Observable.just(1).delay(99, TimeUnit.MILLISECONDS, scheduler);

 assertThat(observable, scheduler)
         .after(100, TimeUnit.MILLISECONDS)
         .values()
         .isNotEmpty();

There's also support for rx.Single and rx.Completable.