KotlinJunit错误:类Kotlin.Unit不能强制转换为类java.lang.Boolean

ljo96ir5  于 12个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(150)

安装:Springboot withKotlin
产品描述:尝试从一个KotlinJunit测试文件中模拟一个java方法
错误源代码行:

Mockito.`when`(workflowService.sendEvent(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(true)

错误消息:

class kotlin.Unit cannot be cast to class java.lang.Boolean 

 class kotlin.Unit cannot be cast to class java.lang.Boolean (kotlin.Unit is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')
ewm0tg9j

ewm0tg9j1#

我的解决方案:使用ArgumentMatchers. any()代替ArgumentMatchers.anyString(),例如:

Mockito.`when`(workflowService.sendEvent(ArgumentMatchers.any(), ArgumentMatchers.any()))
 .thenReturn(true)

因为JavaString参数也接受null,所以我们可以使用ArgumentMatchers.any()

相关问题