Kotlin Mockito仿制药
假设我的课是:
open class TestThis{
@Autowired
private var myService : MyService? = null
fun doMyFunction(){
val result = myService.doSomething("hello world", Function { entry ->
var retVal : Boolean = false
//some processing
retVal
})
}
}
@Service
open class MyService{
fun doSomething(str1 : String, java.util.Function<MyCrap, Boolean>) : List<String>{
//do something here
}
}
@RunWith(MockitoJUnitRunner::class)
class TestThisTest{
@Mock
var myService : MyService? = null
@InjectMocks
var test : TestThis? = null
@Before
fun before(){
val list : List<String> = //init list
//this line causes compilation error due to generics. error 1
Mockito.`when`(myService.doSomething(Mockito.anyString(), Mockito.any(Function::class.java))).thenReturn(list)
//this line also causes compilation error due to generics. error 2
Mockito.`when`(myService.doSomething(Mockito.anyString(), Mockito.any(Function<MyCrap, Boolean>::class.java))).thenReturn(list)
}
}
错误1:
类型推理失败。预期类型不匹配。
错误2:
只允许在类字面的左侧
的左侧
,因此如何模拟myService#dosomething
?
Suppose my class is:
open class TestThis{
@Autowired
private var myService : MyService? = null
fun doMyFunction(){
val result = myService.doSomething("hello world", Function { entry ->
var retVal : Boolean = false
//some processing
retVal
})
}
}
@Service
open class MyService{
fun doSomething(str1 : String, java.util.Function<MyCrap, Boolean>) : List<String>{
//do something here
}
}
@RunWith(MockitoJUnitRunner::class)
class TestThisTest{
@Mock
var myService : MyService? = null
@InjectMocks
var test : TestThis? = null
@Before
fun before(){
val list : List<String> = //init list
//this line causes compilation error due to generics. error 1
Mockito.`when`(myService.doSomething(Mockito.anyString(), Mockito.any(Function::class.java))).thenReturn(list)
//this line also causes compilation error due to generics. error 2
Mockito.`when`(myService.doSomething(Mockito.anyString(), Mockito.any(Function<MyCrap, Boolean>::class.java))).thenReturn(list)
}
}
error 1:
Type inference failed. Expected type mismatch.
error 2:
Only classes are allowed on the left hand side of a class literal
So, how do I mock myService#doSomething
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让匹配者与Kotlin合作可能是一个问题。
mockito.yany()
与它匹配。这是因为它返回null
,并且无法分配给不可删除的参数。只允许在类文字
所有这些问题都可以通过简单
Mockitoutils
类解决:使用示例:
模拟服务:
测试服务:
替代解决方案:
另一个可能的解决方案是使用库 oighito-kotlin 。
maven> maven :
Getting matchers to work with Kotlin can be a problem.
Mockito.any()
. This is because it returnsnull
and this is not assignable to a non-nullable parameter.Only classes are allowed on the left hand side of a class literal
All these problems can be resolved via simple
MockitoUtils
class:Example of usage:
Service to mock:
Service to test:
Test implementation:
Alternative solution:
Another possible solution would be to use a library like mockito-kotlin.
Maven:
当您尝试在此服务中测试某些内容时,您不应嘲笑“测试”。如果您嘲笑它,那么没有任何“内部”。这只是一个模拟。
尝试实现它,然后注入MyService的模拟。
您正在写测试的地方似乎也很奇怪。为什么在测试级测试中的MyService进行单位测试。您应该为MyService创建自己的单位测试。
You should not mock "TestThis" when you try to test something inside this Service. If you mock it there is nothing "inside". It is just a mock.
Try instanciating it and then inject a mock of MyService.
It also seems weird where you are writing your Test. Why is a Unit test for MyService in the testclass TestThisTest. You should creat your own Unit test for MyService.