Java 如何使用TestRestTemplate集成测试RESTful API放置端点?
我目前正在开发一个Spring Boot CRUD RESTful API,它的用户实体由两个参数组成:Java 如何使用TestRestTemplate集成测试RESTful API放置端点?,java,spring,spring-boot,testing,integration-testing,Java,Spring,Spring Boot,Testing,Integration Testing,我目前正在开发一个Spring Boot CRUD RESTful API,它的用户实体由两个参数组成:name和id。其端点为: 在/users中发布请求-创建用户 在/users/{id}中获取请求-按id列出特定用户 在/users中获取请求-列出所有用户 将请求放入/users/{id}-根据特定用户的id更新该用户 删除/users/{id}中的请求-按其id删除特定用户 每个端点都构建有一个控制器和一个服务来实现其逻辑 我已经为我的控制器和服务编写了单元测试,现在我正在尝试构建集
name
和id
。其端点为:
- 在/users中发布请求-创建用户
- 在/users/{id}中获取请求-按id列出特定用户
- 在/users中获取请求-列出所有用户
- 将请求放入/users/{id}-根据特定用户的id更新该用户
- 删除/users/{id}中的请求-按其id删除特定用户
@SpringBootTest(classes = UsersApiApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {
@LocalServerPort
private int port;
TestRestTemplate restTemplate = new TestRestTemplate();
HttpHeaders headers = new HttpHeaders();
private void instantiateNewUser() {
User userNumberFour = new User();
userNumberFour.setName("Four");
userNumberFour.setId(4L);
ResponseEntity<User> responseEntity = restTemplate
.postForEntity(createURLWithPort("/users"), userNumberFour, User.class);
}
@Test
public void createNewUserTest() {
User testUser = new User();
testUser.setName("Test User");
testUser.setId(5L);
ResponseEntity<User> responseEntity = restTemplate
.postForEntity(createURLWithPort("/users"), testUser, User.class);
assertEquals(201, responseEntity.getStatusCodeValue());
assertEquals(responseEntity.getBody(), testUser);
}
@Test
public void listSpecificUserTest() throws JSONException {
instantiateNewUser();
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
createURLWithPort("/users/4/"),
HttpMethod.GET, httpEntity, String.class);
String expectedResponseBody = "{id:4,name:Four}";
assertEquals(200, responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(expectedResponseBody, responseEntity.getBody(), false);
}
@Test
public void listAllUsersTest() throws JSONException {
instantiateNewUser();
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
createURLWithPort("/users"),
HttpMethod.GET, httpEntity, String.class);
//All instantiated users
ArrayList<String> expectedResponseBody = new ArrayList<>(Collections.emptyList());
expectedResponseBody.add("{id:1,name:Neo}");
expectedResponseBody.add("{id:2,name:Owt}");
expectedResponseBody.add("{id:3,name:Three}");
expectedResponseBody.add("{id:4,name:Four}");
assertEquals(200, responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(String.valueOf(expectedResponseBody), responseEntity.getBody(), false);
}
@Test
public void deleteSpecificUserTest() throws JSONException {
instantiateNewUser();
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
createURLWithPort("/users/4/"),
HttpMethod.DELETE, httpEntity, String.class);
assertEquals(204, responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(null, responseEntity.getBody(), false);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
}
@SpringBootTest(class=UsersApiApplication.class,
webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口)
公共类UserControllerTest{
@本地服务器端口
专用int端口;
TestRestTemplate restTemplate=新的TestRestTemplate();
HttpHeaders=新的HttpHeaders();
私有void实例化enewuser(){
User userNumberFour=新用户();
userNumberFour.setName(“四”);
userNumberFour.setId(4L);
ResponseEntity ResponseEntity=restTemplate
.postForEntity(createURLWithPort(“/users”)、userNumberFour、User.class);
}
@试验
public void createNewUserTest(){
用户testUser=新用户();
setName(“测试用户”);
testUser.setId(5L);
ResponseEntity ResponseEntity=restTemplate
.postForEntity(createURLWithPort(“/users”)、testUser、User.class);
assertEquals(201,responseEntity.getStatusCodeValue());
assertEquals(responseEntity.getBody(),testUser);
}
@试验
public void listSpecificUserTest()引发JSONException{
实例化Enewuser();
HttpEntity HttpEntity=新的HttpEntity(空,标题);
ResponseEntity ResponseEntity=restTemplate.exchange(
createURLWithPort(“/users/4/”),
HttpMethod.GET、httpEntity、String.class);
字符串expectedResponseBody=“{id:4,name:Four}”;
assertEquals(200,responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(expectedResponseBody,responseEntity.getBody(),false);
}
@试验
public void listalUserStest()抛出JSONException{
实例化Enewuser();
HttpEntity HttpEntity=新的HttpEntity(空,标题);
ResponseEntity ResponseEntity=restTemplate.exchange(
createURLWithPort(“/users”),
HttpMethod.GET、httpEntity、String.class);
//所有实例化用户
ArrayList expectedResponseBody=新的ArrayList(Collections.emptyList());
expectedResponseBody.add(“{id:1,name:Neo}”);
expectedResponseBody.add(“{id:2,name:Owt}”);
expectedResponseBody.add(“{id:3,name:Three}”);
expectedResponseBody.add(“{id:4,name:Four}”);
assertEquals(200,responseEntity.getStatusCodeValue());
assertEquals(String.valueOf(expectedResponseBody),responseEntity.getBody(),false);
}
@试验
public void deleteSpectUserTest()引发JSONException{
实例化Enewuser();
HttpEntity HttpEntity=新的HttpEntity(空,标题);
ResponseEntity ResponseEntity=restTemplate.exchange(
createURLWithPort(“/users/4/”),
HttpMethod.DELETE,httpEntity,String.class);
assertEquals(204,responseEntity.getStatusCodeValue());
assertEquals(null,responseEntity.getBody(),false);
}
私有字符串createURLWithPort(字符串uri){
返回“http://localhost:“+端口+uri;
}
}
如您所见,它缺少PUT请求方法测试,即更新端点。
为了实现其逻辑,我需要发送一个消息体,其中的内容将覆盖旧用户的特征,但是如何实现呢
这就是我目前所做的:
@Test
public void updateSpecificUserTest() throws JSONException {
instantiateNewUser();
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
createURLWithPort("/users/4/"),
HttpMethod.PUT, httpEntity, String.class);
String expectedResponseBody = "{id:4,name:Four Updated}";
assertEquals(200, responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(expectedResponseBody, responseEntity.getBody(), false);
}
@测试
public void updateSpecificUserTest()引发JSONException{
实例化Enewuser();
HttpEntity HttpEntity=新的HttpEntity(空,标题);
ResponseEntity ResponseEntity=restTemplate.exchange(
createURLWithPort(“/users/4/”),
HttpMethod.PUT、httpEntity、String.class);
字符串expectedResponseBody=“{id:4,name:Four Updated}”;
assertEquals(200,responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(expectedResponseBody,responseEntity.getBody(),false);
}
如果有人能帮上忙,我们将不胜感激,因为他们没有在线找到答案。HttpEntity-HttpEntity=new-HttpEntity(null,headers);
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
您已将正文发送为空。您还可以使用mockMvc,这是一种比rest模板更好的方法
User testUser = new User();
testUser.setName("Test User");
HttpEntity<String> httpEntity = new HttpEntity<String>(testUser, headers);
User testUser=new User();
setName(“测试用户”);
HttpEntity HttpEntity=新的HttpEntity(testUser,标题);
因此,我的问题的解决方案实际上是在我的httpEntity中发送一个空请求主体 我还需要将内容类型设置为JSON:
@Test
public void updateSpecificUserTest() throws JSONException, JsonProcessingException {
instantiateNewUser();
User updatedUser = new User();
updatedUser.setName("Updated");
updatedUser.setId(4L);
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(updatedUser);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<String>(requestBody, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
createURLWithPort("/users/4/"),
HttpMethod.PUT, httpEntity, String.class);
String expectedResponseBody = "{id:4,name:Updated}";
assertEquals(200, responseEntity.getStatusCodeValue());
JSONAssert.assertEquals(expectedResponseBody, responseEntity.getBody(), false);
}
@测试
public void updateSpecificUserTest()引发JSONException,JsonProcessingException{
实例化Enewuser();
用户updateUser=新用户();
updateUser.setName(“更新”);
更新用户设置ID(4L);
ObjectMapper mapper=新的ObjectMapper();
String requestBody=mapper.writeValueAsString(updateUser);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity HttpEntity=新的HttpEntity(需求