使用com.nhaarman.mockitokotlin2測驗restTemplate.getForObject(url, Int::class.java),網址是型別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 的新手
uj5u.com熱心網友回復:
您需要告訴 Mockito 它應該期待 aString作為第一個引數。請嘗試以下操作:
val restTemplate = mock<RestTemplate> {
on { getForObject(anyString(), eq(Int::class.java)) } doReturn 200
}
您可以anyString()在參考檔案中閱讀更多相關資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383401.html
