This library provides some code-style cookies and tries to solve the problem with null values on matching
val list = mock<MutableList<String>>()
list.add("String 1")
list.add("String 2")
list.verify {
times(2).add(any())
}
val list = MutableList::class.mock {
on { size }.thenReturn(5)
on { this[eq(3)] }.thenReturn("String 4")
}
assertEquals(5, list.size)
assertEquals("String 4", list[3])
val list = ArrayList<String>().spy()
val set = spy<HashSet>() {
...
}
// Mocking
val mockService = mock<ServiceClass>()
// Test
mockService.foo()
mockService.bar(SomeData("Test", 1))
// Verification
mockService.verifyOnce { match ->
foo()
bar(match.eq(SomeData("Test", 1)))
}
Standard Mockito API does not always work fine due to the strict control null references in Kotlin. Mockito Kt tries to find default not null value, but in case when it impossible developer can register his own defaults manually. There is two options: local - for one mock object or globally - for all cases.
mock<Foo> {
defaults.register<Bar>(someBarValue)
...
}
Defaults.Global.register<Bar>(someBarValue)
| Java | 1.8+ |
| Maven | v3+ |
| Kotlin | 1.3 |
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-paslavsky-maven</id>
<name>bintray</name>
<url>https://dl.bintray.com/paslavsky/maven</url>
</repository>
</repositories>
<dependency>
<groupId>net.paslavsky.kotlin</groupId>
<artifactId>mockito-kt</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
| Java | 1.8+ |
| Gradle | v3+ |
| Kotlin | 1.3 |
repositories {
maven {
url "https://dl.bintray.com/paslavsky/maven"
}
}
dependencies {
testCompile 'net.paslavsky.kotlin:mockito-kt:2.0.1'
}
In the second version, I changed the signature of the following methods:
fun <T : Any> mock(kClass: KClass<T>, setup: Mock<T>.() -> Unit = {}): T |
-> | fun <T : Any> KClass<T>.mock(setup: Mock<T>.() -> Unit = {}): T |
fun <T : Any> spy(obj: T, setup: Mock<T>.() -> Unit = {}): T |
-> | fun <T : Any> T.spy(setup: Mock<T>.() -> Unit = {}): T |
fun <T : Any> spy(classToSpy: KClass<T>, setup: Mock<T>.() -> Unit = {}): T |
-> | fun <T : Any> KClass<T>.spy(setup: Mock<T>.() -> Unit = {}): T |
fun <T : Any> verify(mock: T, verify: Verification<T>.() -> Unit) |
-> | fun <T : Any> T.verify(verify: Verification<T>.() -> Unit) |
fun <T: Any> verifyOnce(mock: T, checks: T.(match: MatchersKt) -> Unit) |
-> | fun <T : Any> T.verifyOnce(checks: T.(match: MatchersKt) -> Unit) |
It impossible to override old methods and mark as @Deprecated because from Java byte code perspective it's
the same signature.