java - 使用 TestRestTemplate 和 MockRestServiceServer 时,解析异常而不是实体列表不起作用

标签 java json kotlin jsonparser mockrestserviceserver

我有一个简单的 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 while extracting response for type [java.util.List<? extends model.Profile>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.util.ArrayList<model.Profile> from Object value (token JsonToken.START_OBJECT); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.ArrayList<model.Profile> from Object value (token JsonToken.START_OBJECT) at [Source: (PushbackInputStream); line: 1, column: 1]


接下来的 2 个测试是解决方法,但对我来说都不好。
@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。
我期待抛出我的 MyClientCallException 并捕获它。 (它返回500,这很好,但我想看到自己的异常。这只是一个例子,也许我想返回不同的错误代码)
@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 ,我只想要一个字符串作为响应会伪造测试。
有什么办法可以使它更好或解决这个问题?
您可以查看完整的项目:https://github.com/joergi/tryouts/tree/main/kotlin-mockrestserver为了更好的试用。
附言我知道,这个问题类似于Spring TestRestTemplate parse Error instead of HttpClientErrorException thrown但它仍然没有得到答复。

最佳答案

然后返回的异常无法解析到列表,然后给出以下错误: - 您要求 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/

相关文章:

java - 将两个 HashMap 组合成第三个

node.js - 基于键包含搜索 json 列表内部的有效方法

android - 如何从 Flutter PlatformView(Android kotlin 端)返回我的原生 android Activity

java - 如何在 Neo4j 中查找/匹配/选择标识符名称

java - 如何 - Flex 客户端和 Java 作为服务器端

java - 从java transient 或@Transient注释隐藏JSON序列化属性的最佳方法?

javascript - Json 获取的对象显示未定义

java - 如何在 StringBuilder 中将项目与 JsonObject 分开?

java - Kotlin:相当于 Swift 的属性包装器

Android Jetpack Compose 尝试对齐框内的文本