3

I am trying to verify a particular argument among multiple arguments. The other arguments I wish to verify are of a simple nature. But one argument I wish to use check { } to verify it thoroughly. This fails with an error. I have put down the code and the error.

Test:

internal class UserInfoServiceTest: StringSpec() {
init {
    "should return userinfo from auth server" {
        val userInfo = UserInfo("Sethu", "pictureUrl")
        val restTemplate: RestTemplate = mock {
            on { exchange(
                    eq("authurl/userInfo"),
                    eq(HttpMethod.GET),
                    any(),
                    eq(UserInfo::class.java))
            } doReturn
                    ResponseEntity(UserInfo("Sethu", "pictureUrl"),
                                   HttpStatus.OK)
        }
        val userInfoService = UserInfoService(restTemplate, "authurl")

        val returnedUserInfo = userInfoService.getUserInfoFromAuth("accessToken")

        returnedUserInfo shouldBe userInfo

        verify(restTemplate).exchange(eq("authurl/userInfo"),
                eq(HttpMethod.GET),
                check<HttpEntity<Void>> {  httpEntity ->
                    httpEntity.headers.getValue("Authorization") shouldBe "Bearer accessToken"
                },
                eq(UserInfo::class.java))
    }
}

}

Subject:

data class UserInfo(val name: String, val picture: String?)

@Service
class UserInfoService(val restTemplate: RestTemplate,
                  @Value(value = "\${auth0.issuer}") val authServerUrl: String) {

fun getUserInfoFromAuth(accessToken: String): UserInfo {
    val headers = HttpHeaders()
    headers.set(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
    val request = HttpEntity<Void>(headers)
    val response = restTemplate.exchange("$authServerUrl/userInfo", HttpMethod.GET, request, UserInfo::class.java)
    return response.body!!
}
}

Error:

Argument(s) are different! Wanted:
restTemplate.exchange(
    "authurl/userInfo",
    GET,
    <custom argument matcher>,
    class csmart.api.user.UserInfo
);
-> at csmart.api.user.UserInfoServiceTest$1.invokeSuspend(UserInfoServiceTest.kt:29)
Actual invocation has different arguments:
restTemplate.exchange(
    "authurl/userInfo",
    GET,
    <[Authorization:"Bearer accessToken"]>,
    class csmart.api.user.UserInfo
);
-> at csmart.api.user.UserInfoService.getUserInfoFromAuth(UserInfoService.kt:21)
sethu
  • 8,181
  • 7
  • 39
  • 65
  • I have moved to using the mockk library for mocking in kotlin. Its pretty solid and more mature thank mockito. – sethu May 09 '19 at 07:39

0 Answers0