Skip to content

Commit 48fc1aa

Browse files
committed
Merge pull request #16 from nhaarman/release-0.3.0
Release 0.3.0
2 parents 624b7c4 + fc95068 commit 48fc1aa

File tree

13 files changed

+468
-49
lines changed

13 files changed

+468
-49
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Thank you for submitting a pull request! But first:
2+
3+
- [ ] Can you back your code up with tests?
4+
- [ ] Keep your commits clean: [squash your commits if necessary](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History).
5+
- [ ] Make sure you're targeting the `dev` branch with the PR.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip

mockito-kotlin/src/test/kotlin/MatcherTest.kt renamed to mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/ArgumentCaptor.kt

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,13 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
import com.nhaarman.mockito_kotlin.argThat
27-
import com.nhaarman.mockito_kotlin.mock
28-
import com.nhaarman.mockito_kotlin.verify
29-
import org.junit.Test
26+
package com.nhaarman.mockito_kotlin
3027

31-
class MatcherTest {
28+
import org.mockito.ArgumentCaptor
3229

33-
@Test
34-
fun argThat() {
35-
/* Given */
36-
val testClass: TestClass = mock()
37-
38-
/* When */
39-
testClass.go(listOf("test"))
40-
41-
/* Then */
42-
verify(testClass).go(
43-
argThat {
44-
size == 1
45-
get(0) == "test"
46-
}
47-
)
48-
}
49-
50-
interface TestClass {
51-
52-
fun go(v: List<String>)
53-
}
30+
inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)
31+
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
32+
inline fun <reified T : Any> capture(noinline consumer: (T) -> Unit): T {
33+
var times = 0
34+
return argThat { if (++times == 1) consumer.invoke(this); true }
5435
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private fun <T : Any> KClass<T>.toArrayInstance(): T {
100100
"LongArray" -> longArrayOf()
101101
"DoubleArray" -> doubleArrayOf()
102102
"FloatArray" -> floatArrayOf()
103-
else -> throw UnsupportedOperationException("Cannot create a generic array for $simpleName. Use createArrayInstance() instead.")
103+
else -> throw UnsupportedOperationException("Cannot create a generic array for $simpleName. Use createArrayInstance() or anyArray() instead.")
104104
} as T
105105
}
106106

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

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,78 @@
2525

2626
package com.nhaarman.mockito_kotlin
2727

28+
import org.mockito.MockSettings
2829
import org.mockito.Mockito
30+
import org.mockito.invocation.InvocationOnMock
31+
import org.mockito.stubbing.Answer
2932
import org.mockito.verification.VerificationMode
3033
import kotlin.reflect.KClass
3134

32-
inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
33-
fun <T : Any> spy(value: T) = Mockito.spy(value)
35+
fun after(millis: Long) = Mockito.after(millis)
3436

35-
fun <T> whenever(methodCall: T) = Mockito.`when`(methodCall)
36-
fun <T> verify(mock: T) = Mockito.verify(mock)
37-
fun <T> verify(mock: T, mode: VerificationMode) = Mockito.verify(mock, mode)
38-
fun <T> verifyNoMoreInteractions(mock: T) = Mockito.verifyNoMoreInteractions(mock)
39-
fun <T> reset(mock: T) = Mockito.reset(mock)
37+
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
38+
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
39+
inline fun <reified T : Any> anyCollection(): Collection<T> = Mockito.anyCollectionOf(T::class.java)
40+
inline fun <reified T : Any> anyList(): List<T> = Mockito.anyListOf(T::class.java)
41+
inline fun <reified T : Any> anySet(): Set<T> = Mockito.anySetOf(T::class.java)
42+
inline fun <reified K : Any, reified V : Any> anyMap(): Map<K, V> = Mockito.anyMapOf(K::class.java, V::class.java)
43+
inline fun <reified T : Any> anyVararg() = Mockito.anyVararg<T>() ?: createInstance<T>()
4044

41-
fun inOrder(vararg value: Any) = Mockito.inOrder(*value)
42-
fun never() = Mockito.never()
45+
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
46+
47+
fun atLeast(numInvocations: Int) = Mockito.atLeast(numInvocations)
48+
fun atLeastOnce() = Mockito.atLeastOnce()
49+
fun atMost(maxNumberOfInvocations: Int) = Mockito.atMost(maxNumberOfInvocations)
50+
fun calls(wantedNumberOfInvocations: Int) = Mockito.calls(wantedNumberOfInvocations)
51+
52+
fun <T> clearInvocations(vararg mocks: T) = Mockito.clearInvocations(*mocks)
53+
fun description(description: String) = Mockito.description(description)
54+
55+
fun <T> doAnswer(answer: (InvocationOnMock) -> T?) = Mockito.doAnswer { answer(it) }
56+
57+
fun doCallRealMethod() = Mockito.doCallRealMethod()
58+
fun doNothing() = Mockito.doNothing()
59+
fun doReturn(value: Any) = Mockito.doReturn(value)
60+
fun doReturn(toBeReturned: Any, vararg toBeReturnedNext: Any) = Mockito.doReturn(toBeReturned, *toBeReturnedNext)
61+
fun doThrow(toBeThrown: KClass<out Throwable>) = Mockito.doThrow(toBeThrown.java)
62+
fun doThrow(vararg toBeThrown: Throwable) = Mockito.doThrow(*toBeThrown)
4363

4464
inline fun <reified T : Any> eq(value: T) = Mockito.eq(value) ?: createInstance<T>()
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>()
65+
fun ignoreStubs(vararg mocks: Any) = Mockito.ignoreStubs(*mocks)
66+
fun inOrder(vararg mocks: Any) = Mockito.inOrder(*mocks)
67+
68+
inline fun <reified T : Any> isA() = Mockito.isA(T::class.java)
69+
inline fun <reified T : Any> isNotNull() = Mockito.isNotNull(T::class.java)
4770
inline fun <reified T : Any> isNull(): T? = Mockito.isNull(T::class.java)
4871

49-
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = argThat(T::class, predicate)
72+
inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
73+
inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>) = Mockito.mock(T::class.java, defaultAnswer)
74+
inline fun <reified T : Any> mock(s: MockSettings) = Mockito.mock(T::class.java, s)
75+
inline fun <reified T : Any> mock(s: String) = Mockito.mock(T::class.java, s)
76+
77+
fun mockingDetails(toInspect: Any) = Mockito.mockingDetails(toInspect)
78+
fun never() = Mockito.never()
79+
inline fun <reified T : Any> notNull() = Mockito.notNull(T::class.java)
80+
fun only() = Mockito.only()
81+
fun <T> refEq(value: T, vararg excludeFields: String) = Mockito.refEq(value, *excludeFields)
82+
83+
fun reset() = Mockito.reset<Any>()
84+
fun <T> reset(vararg mocks: T) = Mockito.reset(*mocks)
85+
86+
fun <T> same(value: T) = Mockito.same(value)
5087

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)
88+
inline fun <reified T : Any> spy() = Mockito.spy(T::class.java)
89+
fun <T> spy(value: T) = Mockito.spy(value)
90+
91+
fun <T> stub(methodCall: T) = Mockito.stub(methodCall)
92+
fun timeout(millis: Long) = Mockito.timeout(millis)
93+
fun times(numInvocations: Int) = Mockito.times(numInvocations)
94+
fun validateMockitoUsage() = Mockito.validateMockitoUsage()
95+
96+
fun <T> verify(mock: T) = Mockito.verify(mock)
97+
fun <T> verify(mock: T, mode: VerificationMode) = Mockito.verify(mock, mode)
98+
fun <T> verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteractions(*mocks)
99+
fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks)
100+
101+
fun <T> whenever(methodCall: T) = Mockito.`when`(methodCall)
102+
fun withSettings() = Mockito.withSettings()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.stubbing.Stubber
29+
30+
fun <T> Stubber.whenever(mock: T) = `when`(mock)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.nhaarman.mockito_kotlin.argumentCaptor
2+
import com.nhaarman.mockito_kotlin.mock
3+
import com.nhaarman.mockito_kotlin.verify
4+
import com.nhaarman.expect.expect
5+
import com.nhaarman.mockito_kotlin.capture
6+
import org.junit.Test
7+
import java.util.*
8+
9+
class ArgumentCaptorTest {
10+
11+
@Test
12+
fun explicitCaptor() {
13+
val date: Date = mock()
14+
val time = argumentCaptor<Long>()
15+
16+
date.time = 5L
17+
18+
verify(date).time = capture(time)
19+
expect(time.value).toBe(5L)
20+
}
21+
22+
@Test
23+
fun implicitCaptor() {
24+
val date: Date = mock()
25+
date.time = 5L
26+
27+
verify(date).time = capture {
28+
expect(it).toBe(5L)
29+
}
30+
}
31+
}

mockito-kotlin/src/test/kotlin/Fake.kt renamed to mockito-kotlin/src/test/kotlin/Classes.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,32 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
open class Fake {
27-
open fun go(arg: Any?) {
26+
open class Open {
27+
open fun go(vararg arg: Any?) {
2828
}
29+
30+
open fun modifiesContents(a: IntArray) {
31+
for (i in 0..a.size - 1) {
32+
a[i] = a[i] + 1
33+
}
34+
}
35+
36+
open fun stringResult() = "Default"
37+
}
38+
39+
class Closed
40+
41+
interface Methods {
42+
43+
fun intArray(i: IntArray)
44+
fun closed(c: Closed)
45+
fun closedArray(a: Array<Closed>)
46+
fun closedCollection(c: Collection<Closed>)
47+
fun closedList(c: List<Closed>)
48+
fun closedStringMap(m: Map<Closed, String>)
49+
fun closedSet(s: Set<Closed>)
50+
fun string(s: String)
51+
fun closedVararg(vararg c: Closed)
52+
53+
fun stringResult(): String
2954
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CreateInstanceTest {
158158
@Test(expected = UnsupportedOperationException::class)
159159
fun classArray_usingAny() {
160160
/* When */
161-
createInstance<Array<Fake>>()
161+
createInstance<Array<Open>>()
162162
}
163163

164164
@Test
@@ -371,7 +371,7 @@ class CreateInstanceTest {
371371
private class PrivateClass private constructor(val data: String)
372372

373373
class ClosedClass
374-
class ClosedParameterizedClass(val fake: Fake)
374+
class ClosedParameterizedClass(val open: Open)
375375
class ClosedClosedParameterizedClass(val closed: ClosedParameterizedClass)
376376

377377
class SingleParameterClass(val first: Byte)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class EqTest {
3737
private val openClassInstance: MyClass = MyClass()
3838
private val closedClassInstance: ClosedClass = ClosedClass()
3939

40-
private lateinit var doAnswer: Fake
40+
private lateinit var doAnswer: Open
4141

4242
@Before
4343
fun setup() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
import com.nhaarman.expect.expect
2727
import com.nhaarman.mockito_kotlin.mock
28+
import com.nhaarman.mockito_kotlin.whenever
2829
import org.junit.Test
30+
import org.mockito.Mockito.RETURNS_DEEP_STUBS
2931
import org.mockito.exceptions.base.MockitoException
32+
import java.util.*
3033

3134
class MockTest {
3235

@@ -82,6 +85,13 @@ class MockTest {
8285
mock<ClosedClass>()
8386
}
8487

88+
@Test
89+
fun deepStubs() {
90+
val cal: Calendar = mock(RETURNS_DEEP_STUBS)
91+
whenever(cal.time.time).thenReturn(123L)
92+
expect(cal.time.time).toBe(123L)
93+
}
94+
8595
private interface MyInterface
8696
private open class MyClass
8797
private class ClosedClass

0 commit comments

Comments
 (0)