Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.krossovochkin.butterknifetest
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import org.junit.Test
class RxUnitTest {
/* Base tests */
@Test
fun `noEmitAndComplete() completes without emitting value`() {
noEmitAndComplete().test()
.assertNoValues()
.assertComplete()
}
@Test
fun `emitItemAndComplete() emits item and completes`() {
emitItemAndComplete().test()
.assertValue(ITEM)
.assertComplete()
}
@Test
fun `emitItemAndNotComplete() emits item and not complete`() {
emitItemAndNotComplete().test()
.assertValue(ITEM)
.assertNotComplete()
}
@Test
fun `emitMultipleItemsAndComplete() emits items and completes`() {
emitMultipleItemsAndComplete().test()
.assertValues(ITEM_1, ITEM_2)
.assertComplete()
}
@Test
fun `emitMultipleItemsAndNotComplete() emits items and not completes`() {
emitMultipleItemsAndNotComplete().test()
.assertValues(ITEM_1, ITEM_2)
.assertNotComplete()
}
/* singleOrError */
@Test
fun `singleOrError() noEmitAndComplete() terminates with NoSuchElementException`() {
noEmitAndComplete().singleOrError().test()
.assertNoValues()
.assertError(NoSuchElementException::class.java)
}
@Test
fun `singleOrError() emitItemAndComplete() emits one item and completes`() {
emitItemAndComplete().singleOrError().test()
.assertValue(ITEM)
.assertComplete()
}
@Test
fun `singleOrError() emitItemAndNotComplete() emits nothing and not complete`() {
emitItemAndNotComplete().singleOrError().test()
.assertNoValues()
.assertNoErrors()
.assertNotComplete()
}
@Test
fun `singleOrError() emitMultipleItemsAndComplete() terminates with IllegalArgumentException`() {
emitMultipleItemsAndComplete().singleOrError().test()
.assertNoValues()
.assertError(IllegalArgumentException::class.java)
}
@Test
fun `singleOrError() emitMultipleItemsAndNotComplete() terminates with IllegalArgumentException`() {
emitMultipleItemsAndNotComplete().singleOrError().test()
.assertNoValues()
.assertError(IllegalArgumentException::class.java)
}
/* firstOrError */
@Test
fun `firstOrError() noEmitAndComplete() terminates with NoSuchElementException`() {
noEmitAndComplete().firstOrError().test()
.assertNoValues()
.assertError(NoSuchElementException::class.java)
}
@Test
fun `firstOrError() emitItemAndComplete() emits item and completes`() {
emitItemAndComplete().firstOrError().test()
.assertValue(ITEM)
.assertNoErrors()
.assertComplete()
}
@Test
fun `firstOrError() emitItemAndNotComplete() emits item and completes`() {
emitItemAndNotComplete().firstOrError().test()
.assertValue(ITEM)
.assertNoErrors()
.assertComplete()
}
@Test
fun `firstOrError() emitMultipleItemsAndComplete() emits item and completes`() {
emitMultipleItemsAndComplete().firstOrError().test()
.assertValue(ITEM_1)
.assertNoErrors()
.assertComplete()
}
@Test
fun `firstOrError() emitMultipleItemsAndNotComplete() emits item and completes`() {
emitMultipleItemsAndNotComplete().firstOrError().test()
.assertValue(ITEM_1)
.assertNoErrors()
.assertComplete()
}
/* first */
@Test
fun `first() noEmitAndComplete() emits default item and completes`() {
noEmitAndComplete().first(DEFAULT_ITEM).test()
.assertValue(DEFAULT_ITEM)
.assertComplete()
}
@Test
fun `first() emitAndComplete() emits item and completes`() {
emitItemAndComplete().first(DEFAULT_ITEM).test()
.assertValue(ITEM)
.assertComplete()
}
@Test
fun `first() emitAndNotComplete() emits item and completes`() {
emitItemAndNotComplete().first(DEFAULT_ITEM).test()
.assertValue(ITEM)
.assertComplete()
}
@Test
fun `first() emitMultipleItemsAndComplete() emits item and completes`() {
emitMultipleItemsAndComplete().first(DEFAULT_ITEM).test()
.assertValue(ITEM_1)
.assertComplete()
}
@Test
fun `first() emitMultipleItemsAndNotComplete() emits item and completes`() {
emitMultipleItemsAndNotComplete().first(DEFAULT_ITEM).test()
.assertValue(ITEM_1)
.assertComplete()
}
/* single */
@Test
fun `single() noEmitAndComplete() emits default item and completes`() {
noEmitAndComplete().single(DEFAULT_ITEM).test()
.assertValue(DEFAULT_ITEM)
.assertComplete()
}
@Test
fun `single() emitItemAndComplete() emits item and completes`() {
emitItemAndComplete().single(DEFAULT_ITEM).test()
.assertValue(ITEM)
.assertComplete()
}
@Test
fun `single() emitItemAndNotComplete() emits nothing and not complete`() {
emitItemAndNotComplete().single(DEFAULT_ITEM).test()
.assertNoValues()
.assertNoErrors()
.assertNotComplete()
}
@Test
fun `single() emitMultipleItemsAndComplete() terminates with IllegalArgumentException`() {
emitMultipleItemsAndComplete().single(DEFAULT_ITEM).test()
.assertNoValues()
.assertError(IllegalArgumentException::class.java)
}
@Test
fun `single() emitMultipleItemsAndNotComplete() terminates with IllegalArgumentException`() {
emitMultipleItemsAndNotComplete().single(DEFAULT_ITEM).test()
.assertNoValues()
.assertError(IllegalArgumentException::class.java)
}
private fun noEmitAndComplete() = Observable.empty<String>()
private fun emitItemAndComplete() = Observable.just(ITEM)
private fun emitItemAndNotComplete() = BehaviorSubject.createDefault(ITEM).hide()
private fun emitMultipleItemsAndComplete() = Observable.just(ITEM_1, ITEM_2)
private fun emitMultipleItemsAndNotComplete() = Observable.just(ITEM_1, ITEM_2)
.concatWith(Observable.never<String>())
companion object {
private const val DEFAULT_ITEM = "default_item"
private const val ITEM = "item"
private const val ITEM_1 = "item1"
private const val ITEM_2 = "item2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment