我正在尝试验证多个参数中的特定参数。我想证实的其他论点很简单。但是有一个论点我希望使用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)