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

Joe*_*erg 5 java json kotlin mockrestserviceserver jsonparser

我有一个简单的控制器(代码

@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"))
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器正在调用 RestClient 进行客户端调用(代码

@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")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

MyClientCallException 看起来像这样(代码

class MyClientCallException(message: String) :
    ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, message)
Run Code Online (Sandbox Code Playgroud)

我的班级中有 3 个功能测试(代码)我使用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
}
Run Code Online (Sandbox Code Playgroud)

此测试按预期工作。客户端正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
}
Run Code Online (Sandbox Code Playgroud)

当我进行客户端调用时,它会假装超时异常,这种情况总是会发生。然后返回的Exceptions不能被解析为List<Profile>然后给出以下错误:

org.springframework.web.client.RestClientException:提取类型 [java.util.List<? 扩展 model.Profile>] 和内容类型 [application/json];嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法java.util.ArrayList<model.Profile>从对象值(令牌JsonToken.START_OBJECT)反序列化类型的 值 ;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法java.util.ArrayList<model.Profile>JsonToken.START_OBJECT[Source: (PushbackInputStream); 处的Object value (token )反序列化类型的值;行:1,列: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
        )
    }

}
Run Code Online (Sandbox Code Playgroud)

响应类型仍与原始类型相同。但它捕获了 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

}
Run Code Online (Sandbox Code Playgroud)

第二个:我只是将所有内容解析为字符串,这有助于解决它。但我最初正在等待List<Profile>,告诉testRestTemplate,我只想要一个字符串作为响应会伪造测试。

有什么办法可以使它更好或解决这个问题?

您可以查看完整的项目:https : //github.com/joergi/tryouts/tree/main/kotlin-mockrestserver以获得更好的试用。

PS 我知道,这个问题类似于Spring TestRestTemplate parse Error 而不是 HttpClientErrorException 抛出,但它仍然没有答案。

use*_*814 1

然后返回的异常无法解析到列表,然后给出以下错误: - 您要求杰克逊将异常映射到配置文件列表,这显然失败了。

为什么不使用响应类型作为 MyClientCallException 而不是 ParameterizedTypeReference<List> ?- 这应该允许 Jackson 将异常响应流解析为 MyClientCallException

另外,如果您想知道为什么RestClientException处理方式与其他客户端错误不同 - https://github.com/spring-projects/spring-framework/commit/b84fefc430cdf10d428b02f6d61009b7460ae26f#diff-309e077b6d47c54260c59a899971042456c5e03eb5e96e51 21c31842b55deedb