Skip to content

A small library that provides helper functions to work with RxJava and Mockito in Kotlin.

License

Notifications You must be signed in to change notification settings

bartek-wesolowski/mockito-kotlin-rx

Repository files navigation

Mockito-Kotlin-Rx

Maven Central build

A small library that provides helper functions to work with RxJava and Mockito in Kotlin.

Example usages

The following function calls from Mockito-Kotlin can now be simplified.

whenever(mock.getStringObservable()).thenReturn(Observable.just("text")) // Mockito-Kotlin
whenever(mock.getStringObservable()).thenReturnJust("text") // Mockito-Kotlin-Rx

whenever(mock.getStringObservable()).thenReturn(Observable.just("text1", "text2", "text3")) // Mockito-Kotlin
whenever(mock.getStringObservable()).thenReturnJust("text1", "text2", "text3") // Mockito-Kotlin-Rx

whenever(mock.getStringObservable()).thenReturn(Observable.empty()) // Mockito-Kotlin
whenever(mock.getStringObservable()).thenReturnEmpty() // Mockito-Kotlin-Rx

whenever(mock.getStringObservable()).thenReturn(Observable.never()) // Mockito-Kotlin
whenever(mock.getStringObservable()).thenReturnNever() // Mockito-Kotlin-Rx

whenever(mock.getStringObservable()).thenReturn(Observable.error(IOException())) // Mockito-Kotlin
whenever(mock.getStringObservable()).thenReturnError(IOException()) // Mockito-Kotlin-Rx

val mock = mock<MyClass> {
    on { getStringObservable() } doReturn Observable.just("text") // Mockito-Kotlin
    on { getStringObservable() } doReturnJust "text" // Mockito-Kotlin-Rx

    on { getStringObservable() } doReturn Observable.just("text1", "text2", "text3") // Mockito-Kotlin
    on { getStringObservable() }.doReturnJust("text1", "text2", "text3") // Mockito-Kotlin-Rx
    
    on { getStringObservable() } doReturn Observable.empty() // Mockito-Kotlin
    on { getStringObservable() }.doReturnEmpty() // Mockito-Kotlin-Rx
    
    on { getStringObservable() } doReturn Observable.never() // Mockito-Kotlin
    on { getStringObservable() }.doReturnNever() // Mockito-Kotlin-Rx
    
    on { getStringObservable() } doReturn Observable.error(IOException()) // Mockito-Kotlin
    on { getStringObservable() } doReturnError IOException() // Mockito-Kotlin-Rx
}

Similar helper functions are available for Flowables, Singles, and Maybes.

Installation

Mockito-Kotlin-Rx is available on Maven Central. Add the following to your build.gradle/build.gradle.kts file:

Groovy DSL

testImplementation "io.github.bartek-wesolowski:mockito-kotlin-rx:2.0.0"

Kotlin DSL

testImplementation("io.github.bartek-wesolowski:mockito-kotlin-rx:2.0.0")