Skip to content

Commit f18d86e

Browse files
authored
Merge pull request #70 from nhaarman/release-0.6.1
Release 0.6.1
2 parents d07a2c9 + 650e293 commit f18d86e

File tree

11 files changed

+72
-116
lines changed

11 files changed

+72
-116
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ install:
1414

1515
script:
1616
- ./gradlew test
17+
18+
notifications:
19+
email: false

README.md

Lines changed: 17 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,96 +17,25 @@ dependencies {
1717
}
1818
```
1919

20-
## Examples
20+
## Example
2121

22-
### Creating mock instances
23-
24-
Due to Kotlin's [reified type parameters](https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters), if the type can be inferred, you don't have to specify it explicitly:
25-
26-
**Java**:
27-
```java
28-
MyClass c = mock(Myclass.class);
29-
c.doSomething(mock(MyOtherClass.class));
30-
```
31-
32-
**Kotlin**:
33-
```kotlin
34-
val c : MyClass = mock()
35-
c.doSomething(mock())
36-
```
37-
38-
If the type can't be inferred, you can pass it like so:
39-
40-
```kotlin
41-
val d = mock<MyClass>()
42-
```
43-
44-
45-
### Expecting any value
46-
47-
Mockito's `any(Class<T>)` often returns `null` for non-primitive classes.
48-
In Kotlin, this can be a problem due to its [null-safety](https://kotlinlang.org/docs/reference/null-safety.html) feature.
49-
This library creates non-null instances when necessary.
50-
Again, if the type can be inferred, you don't have to specify it explicitely:
51-
52-
**Java**:
53-
```java
54-
verify(myClass).doSomething(any(String.class));
55-
```
56-
57-
**Kotlin**:
58-
```kotlin
59-
verify(myClass).doSomething(any()); // Non-nullable parameter type is inferred
60-
```
61-
62-
For generic arrays, use the `anyArray()` method:
63-
64-
```kotlin
65-
verify(myClass).setItems(anyArray())
66-
```
67-
68-
## Custom instance creators
69-
70-
There are some cases where Mockito-Kotlin cannot create an instance of a class.
71-
This can for instance be when a constructor has some specific preconditions
72-
for its parameters.
73-
You can _register_ `instance creators` to overcome this:
74-
75-
```kotlin
76-
MockitoKotlin.registerInstanceCreator<MyClass> { MyClass(5) }
77-
```
78-
79-
Whenever MockitoKotlin needs to create an instance of `MyClass`, this function is called,
80-
giving you ultimate control over how these instances are created.
81-
82-
These instance creators work on a per-file basis: for each of your test files
83-
you will need to register them again.
84-
85-
### Argument Matchers
86-
87-
Using higher-order functions, you can write very clear expectations about expected values.
88-
For example:
89-
90-
**Kotlin**:
91-
```kotlin
92-
verify(myClass).setItems(argThat{ size == 2 })
93-
```
94-
95-
### Argument Captors
96-
97-
Argument Captors can be used to capture argument values for further assertions.
98-
For example:
22+
A test using Mockito-Kotlin typically looks like the following:
9923

10024
```kotlin
101-
verify(myClass).setItems(capture { items ->
102-
assertEquals(2, items.size)
103-
assertEquals("test", items[0])
104-
})
25+
@Test
26+
fun a(){
27+
/* Given */
28+
val mock = mock<MyClass> {
29+
on { getText() } doReturn "text"
30+
}
31+
val classUnderTest = ClassUnderTest(mock)
32+
33+
/* When */
34+
classUnderTest.doAction()
35+
36+
/* Then */
37+
verify(mock).doSomething(any())
38+
}
10539
```
10640

107-
### Convenience functions
108-
109-
Most of Mockito's static functions are available as top-level functions.
110-
That means, IDE's like IntelliJ can easily import and autocomplete them, saving you the hassle of manually importing them.
111-
112-
41+
For more info and samples, see the [Wiki](https://github.com/nhaarman/mockito-kotlin/wiki).

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
2-
id "com.jfrog.bintray" version "1.5"
2+
id "com.jfrog.bintray" version "1.7.1"
3+
id 'com.github.ben-manes.versions' version '0.13.0'
34
}
45
apply plugin: 'maven'
56
apply plugin: 'maven-publish'

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.14-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-all.zip

mockito-kotlin/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ repositories {
2020
dependencies {
2121
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2222
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
23-
compile "org.mockito:mockito-core:2.0.99-beta"
23+
compile "org.mockito:mockito-core:2.1.0-beta.125"
2424

2525
/* Tests */
2626
testCompile "junit:junit:4.12"
27-
testCompile "com.nhaarman:expect.kt:0.5.1"
27+
testCompile "com.nhaarman:expect.kt:0.6.0"
2828
}
2929

3030
publishing {

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
5959
kClass.isPrimitive() -> kClass.toDefaultPrimitiveValue()
6060
kClass.isEnum() -> kClass.java.enumConstants.first()
6161
kClass.isArray() -> kClass.toArrayInstance()
62-
kClass.isClassObject() -> kClass.toClassObject()
62+
kClass.isClassObject() -> kClass.toClassObject()
6363
else -> kClass.easiestConstructor().newInstance()
6464
}
6565
}
@@ -68,20 +68,14 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
6868
* Tries to find the easiest constructor which it can instantiate.
6969
*/
7070
private fun <T : Any> KClass<T>.easiestConstructor(): KFunction<T> {
71-
return constructors.firstOrDefault(
72-
{
73-
it.parameters.filter {
74-
it.type.toString().toLowerCase().contains("array")
75-
}.isEmpty()
76-
},
77-
{
78-
constructors.sortedBy { it.parameters.size }.first()
79-
}
80-
)
71+
return constructors
72+
.sortedBy { it.parameters.size }
73+
.withoutArrayParameters()
74+
.firstOrNull() ?: constructors.sortedBy { it.parameters.size }.first()
8175
}
8276

83-
private fun <T> Collection<T>.firstOrDefault(predicate: (T) -> Boolean, default: () -> T): T {
84-
return firstOrNull(predicate) ?: default()
77+
private fun <T> List<KFunction<T>>.withoutArrayParameters() = filter {
78+
it.parameters.filter { parameter -> parameter.type.toString().toLowerCase().contains("array") }.isEmpty()
8579
}
8680

8781
@Suppress("SENSELESS_COMPARISON")

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ import kotlin.reflect.KClass
4040
fun after(millis: Long) = Mockito.after(millis)
4141

4242
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
43-
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
44-
inline fun <reified T : Any> anyCollection(): Collection<T> = Mockito.anyCollectionOf(T::class.java)
45-
inline fun <reified T : Any> anyList(): List<T> = Mockito.anyListOf(T::class.java)
46-
inline fun <reified T : Any> anySet(): Set<T> = Mockito.anySetOf(T::class.java)
47-
inline fun <reified K : Any, reified V : Any> anyMap(): Map<K, V> = Mockito.anyMapOf(K::class.java, V::class.java)
48-
inline fun <reified T : Any> anyVararg() = Mockito.anyVararg<T>() ?: createInstance<T>()
49-
43+
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
44+
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()
5045
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
5146

5247
fun atLeast(numInvocations: Int): VerificationMode = Mockito.atLeast(numInvocations)!!
@@ -71,8 +66,8 @@ fun ignoreStubs(vararg mocks: Any): Array<out Any> = Mockito.ignoreStubs(*mocks)
7166
fun inOrder(vararg mocks: Any): InOrder = Mockito.inOrder(*mocks)!!
7267

7368
inline fun <reified T : Any> isA(): T? = Mockito.isA(T::class.java)
74-
inline fun <reified T : Any> isNotNull(): T? = Mockito.isNotNull(T::class.java)
75-
inline fun <reified T : Any> isNull(): T? = Mockito.isNull(T::class.java)
69+
fun <T : Any> isNotNull(): T? = Mockito.isNotNull()
70+
fun <T : Any> isNull(): T? = Mockito.isNull()
7671

7772
inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)!!
7873
inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(T::class.java, defaultAnswer)!!
@@ -93,7 +88,7 @@ inline infix fun <reified T> OngoingStubbing<T>.doReturn(ts: List<T>): OngoingSt
9388

9489
fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
9590
fun never(): VerificationMode = Mockito.never()!!
96-
inline fun <reified T : Any> notNull(): T? = Mockito.notNull(T::class.java)
91+
fun <T : Any> notNull(): T? = Mockito.notNull()
9792
fun only(): VerificationMode = Mockito.only()!!
9893
fun <T> refEq(value: T, vararg excludeFields: String): T? = Mockito.refEq(value, *excludeFields)
9994

@@ -115,3 +110,15 @@ fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*
115110

116111
fun <T> whenever(methodCall: T): OngoingStubbing<T> = Mockito.`when`(methodCall)!!
117112
fun withSettings(): MockSettings = Mockito.withSettings()!!
113+
114+
@Deprecated("Use any() instead.", ReplaceWith("any()"))
115+
inline fun <reified T : Any> anyCollection(): Collection<T> = any()
116+
117+
@Deprecated("Use any() instead.", ReplaceWith("any()"))
118+
inline fun <reified T : Any> anyList(): List<T> = any()
119+
120+
@Deprecated("Use any() instead.", ReplaceWith("any()"))
121+
inline fun <reified T : Any> anySet(): Set<T> = any()
122+
123+
@Deprecated("Use any() instead.", ReplaceWith("any()"))
124+
inline fun <reified K : Any, reified V : Any> anyMap(): Map<K, V> = any()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface Methods {
4343
fun intArray(i: IntArray)
4444
fun closed(c: Closed)
4545
fun closedArray(a: Array<Closed>)
46+
fun closedNullableArray(a: Array<Closed?>)
4647
fun closedCollection(c: Collection<Closed>)
4748
fun closedList(c: List<Closed>)
4849
fun closedStringMap(m: Map<Closed, String>)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ class CreateInstanceTest {
424424
}
425425
}
426426

427+
@Test
428+
fun defaultEmptyConstructor_takesSimplestConstructor() {
429+
/* When */
430+
val result = createInstance(WithDefaultEmptyConstructor::class)
431+
432+
/* Then */
433+
expect(result).toNotBeNull()
434+
}
435+
427436
private class PrivateClass private constructor(val data: String)
428437

429438
class ClosedClass
@@ -459,5 +468,9 @@ class CreateInstanceTest {
459468
}
460469
}
461470

471+
class WithDefaultEmptyConstructor() {
472+
constructor(c: ForbiddenConstructor) : this()
473+
}
474+
462475
enum class MyEnum { VALUE, ANOTHER_VALUE }
463476
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ class MockitoKotlinTest {
5454
val result = createInstance<Closed>()
5555

5656
/* Then */
57-
expect(result).toNotBeReferentially(closed)
57+
expect(result).toNotBeTheSameAs(closed)
5858
}
5959
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class MockitoTest {
6464
}
6565
}
6666

67+
@Test
68+
fun anyNullableClassArray() {
69+
mock<Methods>().apply {
70+
closedNullableArray(arrayOf(Closed(), null))
71+
verify(this).closedNullableArray(anyArray())
72+
}
73+
}
74+
6775
@Test
6876
fun anyCollectionOfClosed() {
6977
mock<Methods>().apply {

0 commit comments

Comments
 (0)