From 8257335beb1a0004b70e9cbd4977a37c6dc52771 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Wed, 26 Oct 2016 11:19:23 +0200 Subject: [PATCH] Add `nullableArgumentCaptor` to be able to work with lists of nullables --- .../nhaarman/mockito_kotlin/ArgumentCaptor.kt | 8 +-- .../src/test/kotlin/ArgumentCaptorTest.kt | 50 +++++++++++++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/ArgumentCaptor.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/ArgumentCaptor.kt index cc25b3b2..a759da4c 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/ArgumentCaptor.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/ArgumentCaptor.kt @@ -29,13 +29,14 @@ import org.mockito.ArgumentCaptor import kotlin.reflect.KClass inline fun argumentCaptor(): KArgumentCaptor = KArgumentCaptor(ArgumentCaptor.forClass(T::class.java), T::class) +inline fun nullableArgumentCaptor(): KArgumentCaptor = KArgumentCaptor(ArgumentCaptor.forClass(T::class.java), T::class) inline fun capture(captor: ArgumentCaptor): T = captor.capture() ?: createInstance() @Deprecated("Use captor.capture() instead.", ReplaceWith("captor.capture()"), DeprecationLevel.ERROR) inline fun capture(captor: KArgumentCaptor): T = captor.capture() -class KArgumentCaptor(private val captor: ArgumentCaptor, private val tClass: KClass) { +class KArgumentCaptor(private val captor: ArgumentCaptor, private val tClass: KClass<*>) { val value: T get() = captor.value @@ -43,7 +44,8 @@ class KArgumentCaptor(private val captor: ArgumentCaptor, privat val allValues: List get() = captor.allValues - fun capture(): T = captor.capture() ?: createInstance(tClass) + @Suppress("UNCHECKED_CAST") + fun capture(): T = captor.capture() ?: createInstance(tClass) as T } /** @@ -51,7 +53,7 @@ class KArgumentCaptor(private val captor: ArgumentCaptor, privat * Instead, use [argumentCaptor] in the traditional way, or use one of * [argThat], [argForWhich] or [check]. */ -@Deprecated("Use argumentCaptor() or argThat() instead.", ReplaceWith("check(consumer)"), DeprecationLevel.ERROR) +@Deprecated("Use argumentCaptor(), argThat() or check() instead.", ReplaceWith("check(consumer)"), DeprecationLevel.ERROR) inline fun capture(noinline consumer: (T) -> Unit): T { var times = 0 return argThat { if (++times == 1) consumer.invoke(this); true } diff --git a/mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt b/mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt index 5a176dcf..54aefbd2 100644 --- a/mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt +++ b/mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt @@ -1,15 +1,12 @@ import com.nhaarman.expect.expect -import com.nhaarman.mockito_kotlin.argumentCaptor -import com.nhaarman.mockito_kotlin.mock -import com.nhaarman.mockito_kotlin.times -import com.nhaarman.mockito_kotlin.verify +import com.nhaarman.mockito_kotlin.* import org.junit.Test import java.util.* class ArgumentCaptorTest { @Test - fun explicitCaptor() { + fun argumentCaptor_withSingleValue() { /* Given */ val date: Date = mock() @@ -22,6 +19,34 @@ class ArgumentCaptorTest { expect(captor.value).toBe(5L) } + @Test + fun argumentCaptor_withNullValue_usingNonNullable() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.nullableString(null) + + /* Then */ + val captor = argumentCaptor() + verify(m).nullableString(captor.capture()) + expect(captor.value).toBeNull() + } + + @Test + fun argumentCaptor_withNullValue_usingNullable() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.nullableString(null) + + /* Then */ + val captor = nullableArgumentCaptor() + verify(m).nullableString(captor.capture()) + expect(captor.value).toBeNull() + } + @Test fun argumentCaptor_multipleValues() { /* Given */ @@ -36,4 +61,19 @@ class ArgumentCaptorTest { verify(date, times(2)).time = captor.capture() expect(captor.allValues).toBe(listOf(5, 7)) } + + @Test + fun argumentCaptor_multipleValuesIncludingNull() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.nullableString("test") + m.nullableString(null) + + /* Then */ + val captor = nullableArgumentCaptor() + verify(m, times(2)).nullableString(captor.capture()) + expect(captor.allValues).toBe(listOf("test", null)) + } } \ No newline at end of file