java - 模拟服务器 | 在 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”
解决方案
推荐阅读
- python - Pytorch argmax 跨多个维度
- c - C 中的 Else If 语句未正确执行
- java - 为什么禁用 kubernetes configmap propertysource 不起作用?
- json - 分隔多个对象的 Json 数据...如何按名称访问特定对象
- javascript - 如何使用 reduce() 计算总和?
- php - 我是 Laravel 8 初学者。使用 laravel 8 将表单数据插入数据库
- c++ - 如何模仿 Java 对象级继承以使字面值存在或不存在?
- java - Maven 从一个包运行所有测试
- powershell - 使用 powershell 删除锁定的文件
- php - 在woocommerce中更改数量按钮位置