using mockito-kotlin (<https://github.com/nhaarman...
# announcements
r
using mockito-kotlin (https://github.com/nhaarman/mockito-kotlin) how can I mock the method:
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
n
What seems to be the problem?
r
I am just struggling with generics and syntax:
Copy code
val mockRestTemplate = mock<RestTemplate> {
            val r : ElasticResult<Map<String, Any>>
            on { exchange(any<RequestEntity<String>>(), any<ParameterizedTypeReference<ElasticResult<Map<String, Any>>>>()) }
        }
looks OK, but how do I specify
doReturn r
?
n
the syntax is
on { methodCall() } doReturn result
r
Copy code
val result = mock<ElasticResult<Map<String, Any>>> {}
        val mockRestTemplate = mock<RestTemplate> {
            on { exchange(any<RequestEntity<String>>(),
                    any<ParameterizedTypeReference<ElasticResult<Map<String, Any>>>>()) } doReturn result
        }
gives
Copy code
Error:(29, 91) Kotlin: None of the following functions can be called with the arguments supplied: 
public infix fun <T> OngoingStubbing<???>.doReturn(t: ???): OngoingStubbing<???> defined in com.nhaarman.mockito_kotlin
public fun <T> OngoingStubbing<???>.doReturn(t: ???, vararg ts: ???): OngoingStubbing<???> defined in com.nhaarman.mockito_kotlin
public infix inline fun <reified T> OngoingStubbing<ResponseEntity<ElasticResult<Map<String, Any>>!>!>.doReturn(ts: List<ResponseEntity<ElasticResult<Map<String, Any>>!>!>): OngoingStubbing<ResponseEntity<ElasticResult<Map<String, Any>>!>!> defined in com.nhaarman.mockito_kotlin
n
Your
any<ParameterizedTypeReference<ElasticResult<Map<String, Any>>>>
is invalid, it should be
any<ParameterizedTypeReference<Map<String, Any>>>
r
er, no, the call I am trying to mock looks like this:
Copy code
val request = RequestEntity<String>(body, <http://HttpMethod.POST|HttpMethod.POST>, endpoint)
val respType = object : ParameterizedTypeReference<ElasticResult<Map<String, Any>>>() {}
restTemplate.exchange(request, respType)
n
Then you need to modify
result
to
val result = mock<ResponseEntity<ElasticResult<Map<String, Any>>>>()
Copy code
val result = mock<ResponseEntity<ElasticResult<Map<String, Any>>>> {}
        val mockRestTemplate = mock<RestTemplate> {
            on { exchange(any<RequestEntity<String>>(), any<ParameterizedTypeReference<ElasticResult<Map<String, Any>>>>()) }.doReturn(result)
        }
r
thanks!
👍 1