Skip to content

Instantly share code, notes, and snippets.

@aballano
Created November 16, 2018 09:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aballano/344307d317454d83405afdb3a305e796 to your computer and use it in GitHub Desktop.
Save aballano/344307d317454d83405afdb3a305e796 to your computer and use it in GitHub Desktop.
Mockito & RxJava extensions to facilitate mocking
@JvmName("thenErrorObservable")
fun <T> OngoingStubbing<Observable<T>>.thenError(throwable: Throwable) {
thenReturn(Observable.error(throwable))
}
fun OngoingStubbing<Completable>.thenComplete() {
thenReturn(Completable.complete())
}
@JvmName("thenErrorCompletable")
fun OngoingStubbing<Completable>.thenError(throwable: Throwable) {
thenReturn(Completable.error(throwable))
}
fun <T> OngoingStubbing<Maybe<T>>.thenEmpty() {
thenReturn(Maybe.empty<T>())
}
@JvmName("thenJustMaybe")
fun <T> OngoingStubbing<Maybe<T>>.thenJust(value: T) {
thenReturn(Maybe.just(value))
}
@JvmName("thenErrorMaybe")
fun <T> OngoingStubbing<Maybe<T>>.thenError(throwable: Throwable) {
thenReturn(Maybe.error(throwable))
}
@JvmName("thenJustSingle")
fun <T> OngoingStubbing<Single<T>>.thenJust(value: T) {
thenReturn(Single.just(value))
}
@JvmName("thenJustObservable")
fun <T> OngoingStubbing<Observable<T>>.thenJust(value: T) {
thenReturn(Observable.just(value))
}
@JvmName("thenErrorSingle")
fun <T> OngoingStubbing<Single<T>>.thenError(throwable: Throwable) {
thenReturn(Single.error(throwable))
}
//// Usage
fun someTest() {
// Before
whenever(someRxCallReturningObservable).thenReturn(Observable.just("Hello :)"))
// After
whenever(someRxCallReturningObservable).thenJust("Hello :)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment