使用com.nhaarman.mockitokotlin2
测试restTemplate.getForObject(url, Int::class.java)
,url是String!
的类型。
试图嘲弄RestTemplate::getForObject
val restTemplate = mock<RestTemplate> {
on { getForObject(any(), any()) } doReturn ResponseEntity.ok(Object())
}
但是得到一个错误:
Overload resolution ambiguity. All these functions match.
public open fun <T : Any!> getForObject(url: URI!, responseType: Class<TypeVariable(T)!>!): TypeVariable(T)! defined in org.springframework.web.client.RestTemplate
public open fun <T : Any!> getForObject(url: String!, responseType: Class<TypeVariable(T)!>!, vararg uriVariables: Any!): TypeVariable(T)! defined in org.springframework.web.client.RestTemplate
和
Type mismatch.
Required:
Unit
Found:
ResponseEntity<Object!>!
请帮帮忙,我是科特林新来的
发布于 2021-12-15 16:35:39
你需要告诉莫基托,它应该期待一个String
作为第一次争论。尝试以下几点:
val restTemplate = mock<RestTemplate> {
on { getForObject(anyString(), eq(Int::class.java)) } doReturn 200
}
您可以在anyString()
中阅读更多关于参考文献的内容。
https://stackoverflow.com/questions/70364458
复制相似问题