diff options
author | Niek Haarman <haarman.niek@gmail.com> | 2019-07-05 15:27:49 +0200 |
---|---|---|
committer | Niek Haarman <haarman.niek@gmail.com> | 2019-07-05 15:35:43 +0200 |
commit | 145e0c1b8f2764540400fe75597a40a8941abf9c (patch) | |
tree | 9621540864551252019004a1bfab8cd54e644e07 | |
parent | 5c51a89bd5a3c8c067cc84f10a2e9363d3930269 (diff) | |
download | mockito-kotlin-145e0c1b8f2764540400fe75597a40a8941abf9c.tar.gz |
Manually provide values for primitive types
On the JVM, primitive types get unboxed automatically,
leading to NPE's when the null quirk is used.
3 files changed, 89 insertions, 1 deletions
diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt index 6041238..4d9b5f5 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt @@ -29,7 +29,17 @@ import kotlin.reflect.KClass import java.lang.reflect.Array as JavaArray inline fun <reified T : Any> createInstance(): T { - return createInstance(T::class) + return when (T::class) { + Boolean::class -> false as T + Byte::class -> 0.toByte() as T + Char::class -> 0.toChar() as T + Short::class -> 0.toShort() as T + Int::class -> 0 as T + Long::class -> 0L as T + Float::class -> 0f as T + Double::class -> 0.0 as T + else -> createInstance(T::class) + } } fun <T : Any> createInstance(kClass: KClass<T>): T { diff --git a/tests/src/test/kotlin/test/Classes.kt b/tests/src/test/kotlin/test/Classes.kt index 1572cbd..87b758d 100644 --- a/tests/src/test/kotlin/test/Classes.kt +++ b/tests/src/test/kotlin/test/Classes.kt @@ -53,7 +53,14 @@ interface Methods { fun closedStringMap(m: Map<Closed, String>) fun closedSet(s: Set<Closed>) fun string(s: String) + fun boolean(b: Boolean) + fun byte(b: Byte) + fun char(c: Char) + fun short(s: Short) fun int(i: Int) + fun long(l: Long) + fun float(f: Float) + fun double(d: Double) fun closedVararg(vararg c: Closed) fun throwableClass(t: ThrowableClass) fun nullableString(s: String?) diff --git a/tests/src/test/kotlin/test/MatchersTest.kt b/tests/src/test/kotlin/test/MatchersTest.kt index a17fe35..fa053b4 100644 --- a/tests/src/test/kotlin/test/MatchersTest.kt +++ b/tests/src/test/kotlin/test/MatchersTest.kt @@ -21,6 +21,14 @@ class MatchersTest : TestBase() { } @Test + fun anyInt() { + mock<Methods>().apply { + string("") + verify(this).string(any()) + } + } + + @Test fun anyClosedClass() { mock<Methods>().apply { closed(Closed()) @@ -76,6 +84,69 @@ class MatchersTest : TestBase() { } } + @Test + fun anyNull_forPrimitiveBoolean() { + mock<Methods>().apply { + boolean(false) + verify(this).boolean(anyOrNull()) + } + } + @Test + fun anyNull_forPrimitiveByte() { + mock<Methods>().apply { + byte(3) + verify(this).byte(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveChar() { + mock<Methods>().apply { + char('a') + verify(this).char(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveShort() { + mock<Methods>().apply { + short(3) + verify(this).short(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveInt() { + mock<Methods>().apply { + int(3) + verify(this).int(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveLong() { + mock<Methods>().apply { + long(3) + verify(this).long(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveFloat() { + mock<Methods>().apply { + float(3f) + verify(this).float(anyOrNull()) + } + } + + @Test + fun anyNull_forPrimitiveDouble() { + mock<Methods>().apply { + double(3.0) + verify(this).double(anyOrNull()) + } + } + /** https://github.com/nhaarman/mockito-kotlin/issues/27 */ @Test fun anyThrowableWithSingleThrowableConstructor() { |