aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiek Haarman <haarman.niek@gmail.com>2019-07-05 15:55:47 +0200
committerGitHub <noreply@github.com>2019-07-05 15:55:47 +0200
commitb632b56f714fb003dcc5e4fca71a7e83e49e5c4d (patch)
tree9621540864551252019004a1bfab8cd54e644e07
parent5c51a89bd5a3c8c067cc84f10a2e9363d3930269 (diff)
parent145e0c1b8f2764540400fe75597a40a8941abf9c (diff)
downloadmockito-kotlin-b632b56f714fb003dcc5e4fca71a7e83e49e5c4d.tar.gz
Merge pull request #352 from nhaarman/createinstance-primitivetypes
Manually provide values for primitive types
-rw-r--r--mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt12
-rw-r--r--tests/src/test/kotlin/test/Classes.kt7
-rw-r--r--tests/src/test/kotlin/test/MatchersTest.kt71
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() {