java - 如何在 Spock groovy 中模拟 RequestEntity.put() 和 restTemplate.exchange()
问题描述
api调用的Java代码
我想知道如何测试以下两行代码。
private void api(){
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
RestTemplate restTemplate = new RestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
//And this line
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity,String.class);
HttpStatus StatusCode = responseEntity.getStatusCode();
}
解决方案
这不是一个 Spock 问题本身(您没有提供一条 Spock 规范,所以到目前为止没人知道您尝试了什么),而是一个软件工程或一般测试问题。测试意大利面条代码的问题——这里我的意思是同时做某事并创建许多对象的代码——是从外部无法访问在方法内部创建并存储在局部变量中的对象。有两种方法可以重构代码以获得更好的可测试性:
如果有意义,请更改代码以使用户能够从外部注入依赖项,而不是在内部创建它们的代码。请谷歌“依赖注入”并找到类似的变体
- 构造函数注入,
- 二传手注入,
- 方法参数注入,
- 使用 CDI 等工具/范式时的现场注入。
另一种方法是将方法的对象创建部分分解为较小的生产者(或创建者或工厂)方法,然后您可以在使用部分模拟(间谍)对象时根据您在测试中的选择覆盖(存根)。Spock 提供了这样的间谍,因此您可以轻松使用它们。
我将展示后一种方法以供您参考:
package de.scrum_master.stackoverflow.q58101434;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class MyRestApi {
public HttpStatus api() throws URISyntaxException {
//Code to call an API and i want to test this in groovy spock
HttpHeaders httpHeaders = createHttpHeaders();
RestTemplate restTemplate = createRestTemplate();
String url ="url";
String body ="body";
//How to mock below line
RequestEntity<String> requestEntity = createRequestEntity(url, body);
//And this line
ResponseEntity<String> responseEntity = executeRequest(restTemplate, requestEntity);
return responseEntity.getStatusCode();
}
HttpHeaders createHttpHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return httpHeaders;
}
RestTemplate createRestTemplate() {
return new RestTemplate();
}
RequestEntity<String> createRequestEntity(String url, String body) throws URISyntaxException {
return RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
}
ResponseEntity<String> executeRequest(RestTemplate restTemplate, RequestEntity<String> requestEntity) {
return restTemplate.exchange(requestEntity,String.class);
}
}
看看这个方法现在是如何更有条理和更具可读性(仍然可以改进,我只是以一种快速而肮脏的方式做到了)?请特别注意辅助方法createRequestEntity
和executeRequest
.
现在,您可以编写 Spock 测试:
package de.scrum_master.stackoverflow.q58101434
import org.springframework.http.HttpStatus
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import spock.lang.Specification
import spock.lang.Unroll
class MyRestApiTest extends Specification {
@Unroll
def "API returns status code #statusCode"() {
given: "prepare mocks + spy"
RequestEntity<String> requestEntity = Mock()
ResponseEntity<String> responseEntity = Mock() {
getStatusCode() >> httpStatus
}
MyRestApi myRestApi = Spy() {
createRequestEntity(_, _) >> requestEntity
executeRequest(_, _) >> responseEntity
}
when: "execute API method"
def result = myRestApi.api()
then: "check expected results"
// This actually only tests mockfunctionality, your real test would look differently
statusCode == result.value()
reasonPhrase == result.reasonPhrase
where:
httpStatus | statusCode | reasonPhrase
HttpStatus.OK | 200 | "OK"
HttpStatus.MOVED_PERMANENTLY | 301 | "Moved Permanently"
HttpStatus.UNAUTHORIZED | 401 | "Unauthorized"
}
}
如果您不理解此代码,请随时提出(相关!)后续问题。我建议你了解更多关于干净代码、可测试性、模拟测试的知识,尤其是关于 Spock 的知识。
推荐阅读
- ios - Swift 在 modalPresentationStyle 中隐藏 UIView
- python - 为匀称地安装 GEOS
- java - 使用 Jackson 将 protobuf 转换为 JSON?
- c - 复制 char 数组并从复制的数组打印十六进制会出错
- ruby-on-rails - 索引的圈复杂度太高
- vb.net - 数据库附近的语法不正确
- java - 使用从 XSD 自动生成的 java 类构建 gradle 模块时出现编码错误
- c# - 带有德语变音符号的 C# 中的 SpecFlow 功能
- javascript - 避免使用 html 标签制作元素发送消息
- nagios - Nagios XI API 将更改为 Nagios 核心 API