【发布时间】:2022-01-18 16:25:44
【问题描述】:
使用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!>!
请帮忙,我是 Kotlin 的新手
【问题讨论】:
-
您能添加您要测试的代码吗?谢谢!
-
restTemplate.getForObject(url, Int::class.java),url是字符串类型!
标签: kotlin mockito resttemplate