首页 > 解决方案 > 模拟服务器 | 在 HttpResponse 中返回简单列表

问题描述

我们正在调用一个返回简单列表而不是 DTO/Json 的外部 API,例如ResponseEntity<List<String>>

在我的测试中,我试图模拟响应,mockServer但未能将结果作为 List 返回(只是 String,因为它对我没有帮助):

mockServer.when(restTemplate.getForObject(any(), any()))
            .respond(HttpResponse.response("{\"40\", \"50\"}")
                    .withStatusCode(200)
                    .withDelay(TimeUnit.SECONDS, 1));

String user = "user";
String password = "password";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));
List<String> retList = restTemplate.getForObject(URI, List.class);

我尝试使用[, ], {, },没有一个,双括号,但未能将响应返回为 List..

很高兴知道正确地做到这一点,谢谢!

代码中的 Ps 都适用于真正的 API,响应也以列表的形式出现。问题仅在测试中,错误是:

“无法提取响应:没有找到适合响应类型 [interface java.util.List] 和内容类型 [application/octet-stream] 的 HttpMessageConverter”

标签: javaunit-testingresttemplatemockserver

解决方案


推荐阅读