我正在尝试验证多个论点中的特定论点。我希望验证的其他论点具有简单的性质。但是我希望使用 check { } 来彻底验证它的一个论点。这失败并出现错误。我已经记下了代码和错误。
测试:
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))
}
}
}
主题:
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!!
}
}
错误:
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)