Skip to content

Commit a90618e

Browse files
committed
Support private classes
1 parent 65d6a5f commit a90618e

File tree

6 files changed

+181
-228
lines changed

6 files changed

+181
-228
lines changed

mockito-kotlin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
6868
task sourcesJar(type: Jar) {
6969
from sourceSets.main.allSource
7070
classifier = 'sources'
71-
}
71+
}

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Any.kt

Lines changed: 0 additions & 148 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016 Niek Haarman
5+
* Copyright (c) 2007 Mockito contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.nhaarman.mockito_kotlin
27+
28+
import org.mockito.Answers
29+
import org.mockito.internal.creation.MockSettingsImpl
30+
import org.mockito.internal.util.MockUtil
31+
import java.lang.reflect.Modifier
32+
import java.lang.reflect.ParameterizedType
33+
import java.lang.reflect.Type
34+
import kotlin.reflect.KClass
35+
import kotlin.reflect.KFunction
36+
import kotlin.reflect.KType
37+
import kotlin.reflect.jvm.isAccessible
38+
import kotlin.reflect.jvm.javaType
39+
40+
/**
41+
* A collection of functions that tries to create an instance of
42+
* classes to avoid NPE's when using Mockito with Kotlin.
43+
*/
44+
45+
inline fun <reified T : Any> createInstance() = createInstance(T::class)
46+
47+
fun <T : Any> createInstance(kClass: KClass<T>): T {
48+
return when {
49+
kClass.hasObjectInstance() -> kClass.objectInstance!!
50+
kClass.isMockable() -> kClass.java.uncheckedMock()
51+
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
52+
kClass.isEnum() -> kClass.java.enumConstants.first()
53+
kClass.isArray() -> kClass.toArrayInstance()
54+
else -> kClass.constructors.first().newInstance()
55+
}
56+
}
57+
58+
@Suppress("SENSELESS_COMPARISON")
59+
private fun KClass<*>.hasObjectInstance() = objectInstance != null
60+
61+
private fun KClass<*>.isMockable() = !Modifier.isFinal(java.modifiers)
62+
private fun KClass<*>.isPrimitive() = java.isPrimitive
63+
private fun KClass<*>.isEnum() = java.isEnum
64+
private fun KClass<*>.isArray() = java.isArray
65+
66+
@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_UNIT_OR_ANY")
67+
private fun <T : Any> KClass<T>.toDefaultPrimitiveValue(): T {
68+
return when (simpleName) {
69+
"Byte" -> 0.toByte()
70+
"Short" -> 0.toShort()
71+
"Int" -> 0
72+
"Double" -> 0.0
73+
"Float" -> 0f
74+
"Long" -> 0
75+
"String" -> ""
76+
else -> throw UnsupportedOperationException("Cannot create default primitive for $simpleName.")
77+
} as T
78+
}
79+
80+
@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_UNIT_OR_ANY")
81+
private fun <T : Any> KClass<T>.toArrayInstance(): T {
82+
return when (simpleName) {
83+
"ByteArray" -> byteArrayOf()
84+
"ShortArray" -> shortArrayOf()
85+
"IntArray" -> intArrayOf()
86+
"LongArray" -> longArrayOf()
87+
"DoubleArray" -> doubleArrayOf()
88+
"FloatArray" -> floatArrayOf()
89+
else -> throw UnsupportedOperationException("Cannot create a generic array for $simpleName. Use anyArray() instead.")
90+
} as T
91+
}
92+
93+
private fun <T : Any> KFunction<T>.newInstance(): T {
94+
isAccessible = true
95+
return callBy(parameters.toMap {
96+
it to it.type.createNullableInstance<T>()
97+
})
98+
}
99+
100+
@Suppress("UNCHECKED_CAST")
101+
private fun <T : Any> KType.createNullableInstance(): T? {
102+
if (isMarkedNullable) {
103+
return null
104+
}
105+
106+
val javaType: Type = javaType
107+
return when (javaType) {
108+
is ParameterizedType -> (javaType.rawType as Class<T>).uncheckedMock()
109+
is Class<*> -> createInstance((javaType as Class<T>).kotlin)
110+
else -> null
111+
}
112+
}
113+
114+
/**
115+
* Creates a mock instance of given class, without modifying or checking any internal Mockito state.
116+
*/
117+
@Suppress("UNCHECKED_CAST")
118+
private fun <T> Class<T>.uncheckedMock(): T {
119+
val impl = MockSettingsImpl<T>().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl<*>
120+
val creationSettings = impl.confirm(this)
121+
return MockUtil().createMock(creationSettings) as T
122+
}

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/MatcherExtension.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ fun <T> reset(mock: T) = Mockito.reset(mock)
4141
fun inOrder(vararg value: Any) = Mockito.inOrder(*value)
4242
fun never() = Mockito.never()
4343

44-
inline fun <reified T : Any> eq(value: T) = eq(value, T::class)
45-
fun <T : Any> eq(value: T, kClass: KClass<T>) = Mockito.eq(value) ?: createInstance(kClass)
46-
44+
inline fun <reified T : Any> eq(value: T) = Mockito.eq(value) ?: createInstance()
45+
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
46+
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
4747
inline fun <reified T : Any> isNull(): T? = Mockito.isNull(T::class.java)
48+
49+
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = argThat(T::class, predicate)
50+
51+
@Suppress("UNCHECKED_CAST")
52+
fun <T : Any> argThat(kClass: KClass<T>, predicate: T.() -> Boolean)
53+
= Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(kClass)

0 commit comments

Comments
 (0)