https://kotlinlang.org logo
#announcements
Title
# announcements
r

rrva

04/12/2017, 12:07 PM
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

nhaarman

04/12/2017, 12:12 PM
What seems to be the problem?
r

rrva

04/12/2017, 12:16 PM
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

nhaarman

04/12/2017, 12:17 PM
the syntax is
on { methodCall() } doReturn result
r

rrva

04/12/2017, 12:22 PM
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

nhaarman

04/12/2017, 12:31 PM
Your
any<ParameterizedTypeReference<ElasticResult<Map<String, Any>>>>
is invalid, it should be
any<ParameterizedTypeReference<Map<String, Any>>>
r

rrva

04/12/2017, 12:40 PM
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

nhaarman

04/12/2017, 12:46 PM
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

rrva

04/12/2017, 3:58 PM
thanks!
👍 1
36 Views