java - 模拟新对象
问题描述
我正在尝试使用以下代码段传递我的 responseEntityMock,但它不起作用。
ResponseEntity<byte[]> response =
new RestTemplate().exchange(url, HttpMethod.POST, requestEntity, byte[].class);
谁能告诉我如何为这段代码传递我自己的 ResponseEntityMock 。谢谢。
我正在使用以下代码:
ResponseEntity<byte[]> responseEntity = PowerMockito.mock(ResponseEntity.class);
RestTemplate restTemplateMock = PowerMockito.mock(RestTemplate.class);
PowerMockito.whenNew(RestTemplate.class).withAnyArguments().thenReturn(restTemplateMock);
when(restTemplateMock.exchange(eq(path), eq(POST), any(),
eq(byte[].class))).thenReturn(responseEntity);
解决方案
推荐阅读
- rust - 是否可以用迭代器替换将每个项目的成员变量推入 Vec 的 for 循环?
- python - 从多个 URL 的字符串中检索特定的 URL
- javascript - React 的 Google Analytics 设置
- sql - 查询给我错误消息:无法绑定多部分标识符“llpeople_tbl.Person_ecounsel”
- python - 如何将 ElementTree 写入 Python 中的文件对象?
- apache-kafka - 将具有区分大小写名称的 Oracle 表中的数据加载到 Kafka
- python - 如何使用 opencv 或 PIL 使用原始目标图像颜色为图像蒙版的部分着色?
- php - 如何使用 cronjob 生成谷歌访问令牌?
- hl7-fhir - 使用 HAPI 库针对 https://www.hl7.org/fhir/validation.html 中列出的不同方面验证 FHIR 资源
- proxy - Kerberos 创建 spn 和 keytab 以包含代理 DNS