Skip to content

Commit 145e0c1

Browse files
committed
Manually provide values for primitive types
On the JVM, primitive types get unboxed automatically, leading to NPE's when the null quirk is used.
1 parent 5c51a89 commit 145e0c1

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/internal/CreateInstance.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ import kotlin.reflect.KClass
2929
import java.lang.reflect.Array as JavaArray
3030

3131
inline fun <reified T : Any> createInstance(): T {
32-
return createInstance(T::class)
32+
return when (T::class) {
33+
Boolean::class -> false as T
34+
Byte::class -> 0.toByte() as T
35+
Char::class -> 0.toChar() as T
36+
Short::class -> 0.toShort() as T
37+
Int::class -> 0 as T
38+
Long::class -> 0L as T
39+
Float::class -> 0f as T
40+
Double::class -> 0.0 as T
41+
else -> createInstance(T::class)
42+
}
3343
}
3444

3545
fun <T : Any> createInstance(kClass: KClass<T>): T {

tests/src/test/kotlin/test/Classes.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ interface Methods {
5353
fun closedStringMap(m: Map<Closed, String>)
5454
fun closedSet(s: Set<Closed>)
5555
fun string(s: String)
56+
fun boolean(b: Boolean)
57+
fun byte(b: Byte)
58+
fun char(c: Char)
59+
fun short(s: Short)
5660
fun int(i: Int)
61+
fun long(l: Long)
62+
fun float(f: Float)
63+
fun double(d: Double)
5764
fun closedVararg(vararg c: Closed)
5865
fun throwableClass(t: ThrowableClass)
5966
fun nullableString(s: String?)

tests/src/test/kotlin/test/MatchersTest.kt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class MatchersTest : TestBase() {
2020
}
2121
}
2222

23+
@Test
24+
fun anyInt() {
25+
mock<Methods>().apply {
26+
string("")
27+
verify(this).string(any())
28+
}
29+
}
30+
2331
@Test
2432
fun anyClosedClass() {
2533
mock<Methods>().apply {
@@ -76,6 +84,69 @@ class MatchersTest : TestBase() {
7684
}
7785
}
7886

87+
@Test
88+
fun anyNull_forPrimitiveBoolean() {
89+
mock<Methods>().apply {
90+
boolean(false)
91+
verify(this).boolean(anyOrNull())
92+
}
93+
}
94+
@Test
95+
fun anyNull_forPrimitiveByte() {
96+
mock<Methods>().apply {
97+
byte(3)
98+
verify(this).byte(anyOrNull())
99+
}
100+
}
101+
102+
@Test
103+
fun anyNull_forPrimitiveChar() {
104+
mock<Methods>().apply {
105+
char('a')
106+
verify(this).char(anyOrNull())
107+
}
108+
}
109+
110+
@Test
111+
fun anyNull_forPrimitiveShort() {
112+
mock<Methods>().apply {
113+
short(3)
114+
verify(this).short(anyOrNull())
115+
}
116+
}
117+
118+
@Test
119+
fun anyNull_forPrimitiveInt() {
120+
mock<Methods>().apply {
121+
int(3)
122+
verify(this).int(anyOrNull())
123+
}
124+
}
125+
126+
@Test
127+
fun anyNull_forPrimitiveLong() {
128+
mock<Methods>().apply {
129+
long(3)
130+
verify(this).long(anyOrNull())
131+
}
132+
}
133+
134+
@Test
135+
fun anyNull_forPrimitiveFloat() {
136+
mock<Methods>().apply {
137+
float(3f)
138+
verify(this).float(anyOrNull())
139+
}
140+
}
141+
142+
@Test
143+
fun anyNull_forPrimitiveDouble() {
144+
mock<Methods>().apply {
145+
double(3.0)
146+
verify(this).double(anyOrNull())
147+
}
148+
}
149+
79150
/** https://github.com/nhaarman/mockito-kotlin/issues/27 */
80151
@Test
81152
fun anyThrowableWithSingleThrowableConstructor() {

0 commit comments

Comments
 (0)