我想模拟注释,以检查根据给定注释返回结果的类的良好行为。
下面是我使用OneToOne注释检查良好行为的测试之一:
@Test
fun <T> supports_when_field_has_OneToOne_annotation_with_eager_fetch_type() {
val myHandler = MyHandler<T>()
val joinAnnotation = mock<OneToOne> {
on { this.fetch }.thenReturn(FetchType.EAGER)
onGeneric { this.annotationClass }.thenReturn(OneToOne::class)
}
val supports = myHandler.supports(joinAnnotation)
assertThat(supports).isTrue()
}当我运行我的测试时,我有以下错误:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
KClassImpl不能由annotationType() annotationType()返回
如果你不知道你为什么要克服错误,请继续读下去。由于上述语法的性质,可能会出现问题,因为:
当mockito调用以下代码时,会发生此错误:
onGeneric { this.annotationClass }.thenReturn(OneToOne::class)如果我删除这一行,我就没有问题来模拟注释。(模仿'fetch‘属性非常有效),但我的测试没有通过,因为我需要模拟'annotationClass’
我不明白为什么有错误,为什么错误与annotationType() (java注释方法)有关?
有谁知道怎么解决这个问题吗?
发布于 2020-12-05 16:09:52
我找到了解决问题的办法。
kotlin.Annotation.annotationClass是kotlin扩展函数:
/**
* Returns a [KClass] instance corresponding to the annotation type of this annotation.
*/
public val <T : Annotation> T.annotationClass: KClass<out T>
get() = (this as java.lang.annotation.Annotation).annotationType().kotlin as KClass<out T>此函数调用java.lang.annotation.Annotation.annotationType()
我不能模拟扩展函数,因为kotlin将扩展函数转换为静态方法,而Mockito不能存根静态方法。
所以我直接嘲笑java.lang.annotation.Annotation.annotationType()。
这里是我的新代码:
@Test
fun <T> supports_when_field_has_OneToOne_annotation_with_eager_fetch_type() {
val myHandler = MyHandler<T>()
val joinAnnotation = mock<OneToOne> {
on { this.fetch }.thenReturn(FetchType.EAGER)
on { (this as java.lang.annotation.Annotation).annotationType() }.thenReturn(OneToOne::class.java)
}
val supports = myHandler.supports(joinAnnotation)
assertThat(supports).isTrue()
}有一些替代模拟静态方法(PowerMock,MockK)的方法,但我没有进行测试。
如果有人有更好的建议,我会感兴趣的。
https://stackoverflow.com/questions/65152348
复制相似问题