class HogeTest {
@Test
fun test() {
val listener = mock<(Boolean) -> Unit>()
...
verify(listener)(false)
}
}
次のように @RunWith(AndroidJUnit4::class) をつけるとエラーが発生します。
@RunWith(AndroidJUnit4::class)
class HogeTest {
@Test
fun test() {
val listener = mock<(Boolean) -> Unit>()
...
verify(listener)(false)
}
}
org.mockito.exceptions.base.MockitoException:
ClassCastException occurred while creating the mockito mock :
class to mock : 'kotlin.jvm.functions.Function1', loaded by classloader : 'sun.misc.Launcher$AppClassLoader@18b4aac2'
created class : 'kotlin.jvm.functions.Function1$MockitoMock$1350680399', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@7a2a2c83'
proxy instance class : 'kotlin.jvm.functions.Function1$MockitoMock$1350680399', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@7a2a2c83'
instance creation by : ObjenesisInstantiator
この場合クッションになる interface を定義すると動きます。
@RunWith(AndroidJUnit4::class)
class HogeTest {
private interface Callback : (Boolean) -> Unit
@Test
fun test() {
val listener = mock<Callback>()
...
verify(listener)(false)
}
}
参考 : https://github.com/nhaarman/mockito-kotlin/issues/272
0 件のコメント:
コメントを投稿