use https://github.com/nhaarman/mockito-kotlin
Mockito for Kotlin.
when
is a reserved word of Kotlin. You should write like below on test.
val item = mock(Item::class.java)
`when`(item.length()).thenReturn(10)
hmm... OK. I'll add extensions for Kotlin.
before
var item = mock(Item::class.java)
`when`(item.length()).thenReturn(10)
assertThat(item.length(), `is`(10))
verify(item).length()
after
var item: Item = mock()
item.length().invoked.thenReturn(10)
assertThat(item.length(), `is`(10))
item.verify().length()
I recommend that you also use knit. It is JUnit API for Kotlin.
before
var item = spy(Item(10))
doReturn(11).`when`(item).length()
assertThat(item.length(), `is`(11))
verify(item, times(1)).length()
after
var item = Item(10).spy()
item.doReturn(11).length()
assertThat(item.length(), `is`(11))
item.verify(times(1)).length()
before
model.someMethod(anyInt(), anyString(), any(), any())
.invoked
.thenAnswer {
val a = it.arguments[0] as Int
val b = it.arguments[1] as String
val c = it.arguments[2] as Item
val d = it.arguments[3] as Name
// do something
}
after
model.someMethod(anyInt(), anyString(), any(), any())
.invoked
.thenAnswer {
val (a, b, c, d) = it.arguments4<Int, String, Item, Name>()
// do something
}
This library is distributed by JitPack. Add dependencies your build.gradle
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the dependency
testCompile 'com.github.sys1yagi:kmockito:0.1.2'
Show version
$ ./gradlew version
Bump version
$ ./gradlew bumpMajor
$ ./gradlew bumpMinor
$ ./gradlew bumpPatch
Generate README
$ ./gradlew genReadMe