#RX TestKit: AssertJ for RxJava
This library provides a set of AssertJ assertions that can be used to make unit testing of RxJava code a little easier.
<dependency>
<groupId>com.github.hyleung</groupId>
<artifactId>rx-testkit-java</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
test 'com.github.hyleung:rx-testkit-java:2.0.0'
For RxJava 1.x, the latest release of this library is 1.1.0.
<dependency>
<groupId>com.github.hyleung</groupId>
<artifactId>rx-testkit-java</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
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
.