Befriending Kotlin and Mockito

--

Kotlin and Java are very close friend. A brotherly relationship (although we know Kotlin had the intention are replace Java in Android someday :P). Mockito is a close friend of Java, so naturally it should work well with Kotlin.

But in my experience, I faced several hiccups in their relationship. So we need some work to help mend the relationship :)

Below are some codes to illustration the tensed relationship (you’ll need to run it to see the errors). Firstly the source code of class to be unit tested i.e. SimpleClass and it’s other related classes.

// CLASS FUNCTION TO BE TESTED
class SimpleClass(val injectedClass: InjectedClass) {
fun usingFunction() {
injectedClass.usingDependentObject()
}
fun settingFunction() {
injectedClass.settingDependentObject(DependentClass())
}
}
// CLASS TO BE MOCKED
class InjectedClass() {
lateinit var dependentObject: DependentClass
fun settingDependentObject(dependentObject: DependentClass) {
this.dependentObject = dependentObject
}
fun usingDependentObject() {
this.dependentObject.testFunction()
}
}
// CLASS AS PARAMETER OBJECT
class DependentClass() {
fun testFunction() {
}
}

And then the Test Class

class SimpleClassTest {    lateinit var simpleObject: SimpleClass…

--

--