- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 Controller ( CODE )
@RestController
@RequestMapping("/profiles" , produces = [MediaType.APPLICATION_JSON_VALUE])
class Controller(@Autowired val restClient: RestClient) {
@GetMapping("/simple-get")
fun simpleGetCall(): List<Profile> {
restClient.callClientGet()
return listOf(
Profile("firstname1", "lastname1"),
Profile("firstname2", "lastname2"))
}
}
Controller 正在调用正在进行客户端调用的 RestClient (
CODE )
@Service
class RestClient(@Autowired val restTemplate: RestTemplate) {
fun callClientGet(){
try {
restTemplate.getForEntity(
"/profiles/simple-get",
Number::class.java
)
}catch(exception: Exception){
throw MyClientCallException("this is an exception")
}
}
}
MyClientCallException 看起来像这样 (
CODE )
class MyClientCallException(message: String) :
ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, message)
我的类(class)有 3 个功能测试 (
CODE )
org.springframework.test.web.client.MockRestServiceServer
作为服务器
@Test
fun `when calling service, and client returns Accepted, status should be OK`() {
val responseType: ParameterizedTypeReference<List<Profile>> =
object : ParameterizedTypeReference<List<Profile>>() {}
mockServer?.expect(MockRestRequestMatchers.requestTo(URI("http://localhost:8082/profiles/simple-get")))
?.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
?.andRespond(
MockRestResponseCreators.withStatus(HttpStatus.ACCEPTED)
.contentType(MediaType.APPLICATION_JSON))
val response = testRestTemplate.exchange(
"/profiles/simple-get",
HttpMethod.GET,
null,
responseType
)
response.statusCode shouldBe HttpStatus.OK
response.body?.size shouldBe 2
}
此测试按预期工作。客户回访
ACCEPTED
并且服务器调用返回
List<Profile>
第二次测试失败。
@Test
fun `when calling service, and client timeout, it should return 500 - not working, problems with parsing`() {
val responseType: ParameterizedTypeReference<List<Profile>> =
object : ParameterizedTypeReference<List<Profile>>() {}
mockServer?.expect(MockRestRequestMatchers.requestTo(URI("http://localhost:8082/profiles/simple-get")))
?.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
?.andRespond(
MockRestResponseCreators.withStatus(HttpStatus.REQUEST_TIMEOUT)
.contentType(MediaType.APPLICATION_JSON))
val response = testRestTemplate.exchange(
"/profiles/simple-get",
HttpMethod.GET,
null,
responseType
)
response.statusCode shouldBe HttpStatus.INTERNAL_SERVER_ERROR
}
当我进行客户端调用时,它会假装超时异常,这种情况总是会发生。
Exceptions
无法解析为
List<Profile>
然后给出以下错误:
org.springframework.web.client.RestClientException: Error whileextracting response for type [java.util.List<? extends model.Profile>]and content type [application/json]; nested exception isorg.springframework.http.converter.HttpMessageNotReadableException:JSON parse error: Cannot deserialize value of type
java.util.ArrayList<model.Profile>
from Object value (tokenJsonToken.START_OBJECT
); nested exception iscom.fasterxml.jackson.databind.exc.MismatchedInputException: Cannotdeserialize value of typejava.util.ArrayList<model.Profile>
fromObject value (tokenJsonToken.START_OBJECT
) at [Source:(PushbackInputStream); line: 1, column: 1]
@Test
fun `when calling service, and client timeout, it should return 500 working `() {
val responseType: ParameterizedTypeReference<List<Profile>> =
object : ParameterizedTypeReference<List<Profile>>() {}
mockServer?.expect(MockRestRequestMatchers.requestTo(URI("http://localhost:8082/profiles/simple-get")))
?.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
?.andRespond(
MockRestResponseCreators.withStatus(HttpStatus.REQUEST_TIMEOUT)
.contentType(MediaType.APPLICATION_JSON))
assertThrows<RestClientException> {
testRestTemplate.exchange(
"/profiles/simple-get",
HttpMethod.GET,
null,
responseType
)
}
}
响应类型仍与原始类型相同。但它捕获了 RestClientException。
@Test
fun `when calling service, and client timeout, it should return 500 - but response type is wrong`() {
mockServer?.expect(MockRestRequestMatchers.requestTo(URI("http://localhost:8082/profiles/simple-get")))
?.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
?.andRespond(
MockRestResponseCreators.withStatus(HttpStatus.REQUEST_TIMEOUT)
.contentType(MediaType.APPLICATION_JSON))
val response = testRestTemplate.exchange(
"/profiles/simple-get",
HttpMethod.GET,
null,
String::class.java
)
response.statusCode shouldBe HttpStatus.INTERNAL_SERVER_ERROR
}
第二个:我只是将所有内容解析为字符串,这有助于解决它。
List<Profile>
所以,告诉
testRestTemplate
,我只想要一个字符串作为响应会伪造测试。
最佳答案
然后返回的异常无法解析到列表,然后给出以下错误: - 您要求 jackson 将异常映射到配置文件列表,这显然是失败的。
为什么不使用响应类型作为 MyClientCallException 而不是 ParameterizedTypeReference
? - 这应该允许 jackson 将异常响应流解析为 MyClientCallException
另外,如果您想知道为什么
RestClientException
与其他客户端错误的处理方式不同 -
https://github.com/spring-projects/spring-framework/commit/b84fefc430cdf10d428b02f6d61009b7460ae26f#diff-309e077b6d47c54260c59a899971042456c5e03eb5e96e5121c31842b55deedb
关于java - 使用 TestRestTemplate 和 MockRestServiceServer 时,解析异常而不是实体列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68134196/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!