The following test yields a NullPointerException. Is it not possible to set expectations on a lazy property?
class GarbTest {
@Test
fun xx(){
val aa = Mockito.mock(AA::class.java)
Mockito.`when`(aa.bb).thenReturn("zz")
}
open class AA(){
val bb by lazy { "cc" }
}
}
In your example, AA.bb is final. final/private/equals()/hashCode() methods cannot be stubbed/verified by Mockito. You need to mark bb as open:
open class AA(){
open val bb by lazy { "cc" }
}
You might also consider using nhaarman/mockito-kotlin: Using Mockito with Kotlin. e.g.:
class GarbTest {
@Test
fun xx() {
val aa = mock<AA>() {
on { bb } doReturn "zz"
}
}
open class AA() {
val bb: String = "cc"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With