Skip to content

Commit 9f1df2d

Browse files
committed
Make sure 'eq' does not return null
1 parent 90f6598 commit 9f1df2d

File tree

4 files changed

+158
-6
lines changed

4 files changed

+158
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package com.nhaarman.mockito_kotlin
2727

2828
import org.mockito.Mockito
2929
import org.mockito.verification.VerificationMode
30+
import kotlin.reflect.KClass
3031

3132
inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
3233
fun <T : Any> spy(value: T) = Mockito.spy(value)
@@ -40,6 +41,7 @@ fun <T> reset(mock: T) = Mockito.reset(mock)
4041
fun inOrder(vararg value: Any) = Mockito.inOrder(*value)
4142
fun never() = Mockito.never()
4243

43-
fun <T> eq(value: T) = Mockito.eq(value)
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)
4446

4547
inline fun <reified T : Any> isNull(): T? = Mockito.isNull(T::class.java)

mockito-kotlin/src/test/kotlin/AnyTest.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,6 @@ class AnyTest {
385385
expect(result).toBe(MyEnum.VALUE)
386386
}
387387

388-
open class Fake {
389-
open fun go(arg: Any?) {
390-
}
391-
}
392-
393388
class ClosedClass
394389
class ClosedParameterizedClass(val fake: Fake)
395390
class ClosedClosedParameterizedClass(val closed: ClosedParameterizedClass)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
/*
27+
* The MIT License
28+
*
29+
* Copyright (c) 2016 Niek Haarman
30+
* Copyright (c) 2007 Mockito contributors
31+
*
32+
* Permission is hereby granted, free of charge, to any person obtaining a copy
33+
* of this software and associated documentation files (the "Software"), to deal
34+
* in the Software without restriction, including without limitation the rights
35+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36+
* copies of the Software, and to permit persons to whom the Software is
37+
* furnished to do so, subject to the following conditions:
38+
*
39+
* The above copyright notice and this permission notice shall be included in
40+
* all copies or substantial portions of the Software.
41+
*
42+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
48+
* THE SOFTWARE.
49+
*/
50+
51+
/*
52+
* Copyright 2016 Niek Haarman
53+
*
54+
* Licensed under the Apache License, Version 2.0 (the "License");
55+
* you may not use this file except in compliance with the License.
56+
* You may obtain a copy of the License at
57+
*
58+
* http://www.apache.org/licenses/LICENSE-2.0
59+
*
60+
* Unless required by applicable law or agreed to in writing, software
61+
* distributed under the License is distributed on an "AS IS" BASIS,
62+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
63+
* See the License for the specific language governing permissions and
64+
* limitations under the License.
65+
*/
66+
67+
import com.nhaarman.expect.expect
68+
import com.nhaarman.mockito_kotlin.eq
69+
import com.nhaarman.mockito_kotlin.mock
70+
import org.junit.After
71+
import org.junit.Before
72+
import org.junit.Test
73+
import org.mockito.Mockito
74+
75+
class EqTest {
76+
77+
private val interfaceInstance: MyInterface = MyClass()
78+
private val openClassInstance: MyClass = MyClass()
79+
private val closedClassInstance: ClosedClass = ClosedClass()
80+
81+
private lateinit var doAnswer: Fake
82+
83+
@Before
84+
fun setup() {
85+
/* Create a proper Mockito state */
86+
doAnswer = Mockito.doAnswer { }.`when`(mock())
87+
}
88+
89+
@After
90+
fun tearDown() {
91+
/* Close `any` Mockito state */
92+
doAnswer.go(0)
93+
}
94+
95+
@Test
96+
fun eqInterfaceInstance() {
97+
/* When */
98+
val result = eq(interfaceInstance)
99+
100+
/* Then */
101+
expect(result).toNotBeNull()
102+
}
103+
104+
@Test
105+
fun eqOpenClassInstance() {
106+
/* When */
107+
val result = eq(openClassInstance)
108+
109+
/* Then */
110+
expect(result).toNotBeNull()
111+
}
112+
113+
@Test
114+
fun eqClosedClassInstance() {
115+
/* When */
116+
val result = eq(closedClassInstance)
117+
118+
/* Then */
119+
expect(result).toNotBeNull()
120+
}
121+
122+
private interface MyInterface
123+
private open class MyClass : MyInterface
124+
class ClosedClass
125+
}
126+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
open class Fake {
27+
open fun go(arg: Any?) {
28+
}
29+
}

0 commit comments

Comments
 (0)