Created
November 16, 2018 09:09
-
-
Save aballano/344307d317454d83405afdb3a305e796 to your computer and use it in GitHub Desktop.
Mockito & RxJava extensions to facilitate mocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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