Skip to content

Instantly share code, notes, and snippets.

@krossovochkin
Created May 25, 2019 16:42
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 krossovochkin/8a860ec1c34087518fc086dc40b42bc0 to your computer and use it in GitHub Desktop.
Save krossovochkin/8a860ec1c34087518fc086dc40b42bc0 to your computer and use it in GitHub Desktop.
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