英文:
Spring boot : RestTemplate POST call does not return created status code
问题
我是新手学习Spring Boot Rest Template。我尝试编写用于Rest Template POST调用的单元测试用例。
我已经创建了一个用户控制器来发送虚拟数据。
用户控制器编写的POST调用的方式是否正确,适合单元测试吗?如果不是,正确的编写方式是什么?我从用户控制器返回的响应是否正确?我应该返回状态代码200还是201?
有人可以帮助我吗?
以下是用户控制器:
@PostMapping("/users")
public User createUser(@RequestBody User user){
List<String> messages = new ArrayList<>();
messages.add("Hi");
User createUser = new User();
createUser.setId(1);
createUser.setName("John");
createUser.setAge(22);
createUser.setMessages(messages);
return user;
}
以下是用于POST调用的单元测试用例:
@Test
public void testSuccessPostUser() throws Exception{
String baseUrl = "http://localhost:8080/users";
List<String> messages = new ArrayList<>();
messages.add("Hi");
User user = new User();
user.setId(1);
user.setName("John");
user.setAge(22);
user.setMessages(messages);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<User> httpEntity = new HttpEntity<>(user, httpHeaders);
HttpHandler httpHandler = new HttpHandler();
ResponseEntity<User> actual = httpHandler.sendPost(baseUrl, httpHeaders, user, User.class);
//验证请求成功
assertEquals(HttpStatus.CREATED, actual.getStatusCode());
assertEquals(201, actual.getStatusCodeValue());
assertTrue(responseBody.contains(id));
}
英文:
I'm new to spring boot rest template. I'm trying write unit test case for rest template post call.
I've created a user controller to send the dummy data.
Is the Usercontroller written for POST call is correct for unit test? If not what is the correct way to write it? Is the response I'm returning from usercontroller correct? Should I return status code as 200 or 201?
Can someone help me?
Below is the usercontroller
@PostMapping("/users")
public User createUser(@RequestBody User user){
List<String> messages = new ArrayList<>();
messages.add("Hi");
User createUser = new User();
createUser.setId(1);
createUser.setName("John");
createUser.setAge(22);
createUser.setMessages(messages);
return user;
}
Below id the unit test case for post call:
@Test
public void testSuccessPostUser() throws Exception{
String baseUrl = "http://localhost:8080/users";
List<String> messages = new ArrayList<>();
messages.add("Hi");
User user = new User();
user.setId(1);
user.setName("John");
user.setAge(22);
user.setMessages(messages);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<User> httpEntity = new HttpEntity<>(user, httpHeaders);
HttpHandler httpHandler = new HttpHandler();
ResponseEntity<User> actual = httpHandler.sendPost(baseUrl, httpHeaders, user, User.class);
//verify request succeed
assertEquals(HttpStatus.CREATED, actual.getStatusCode());
assertEquals(201, actual.getStatusCodeValue());
assertTrue(responseBody.contains(id);
}
答案1
得分: 1
POST默认返回200。
如果您希望它返回201,请添加以下内容:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/users")
public User createUser(@RequestBody User user){
英文:
POST returns 200 by default.
If you want to make it return 201 add this:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/users")
public User createUser(@RequestBody User user){
答案2
得分: 1
如果您想返回自定义状态代码,应该返回ResponseEntity
:
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user){
...
return new ResponseEntity(user, HttpStatus.CREATED);
}
如果您想了解POST方法的正确HTTPS状态码,我建议阅读这个链接:https://stackoverflow.com/questions/43327963/what-status-code-should-we-return-if-the-post-succeeds-but-does-not-result-in-cr
英文:
If you want to return custom status codes, you should return RepsponseEntity
:
@PostMapping("/users")
public ResposneEntity<User> createUser(@RequestBody User user){
...
return new ResponseEntity(user, HttpStatus.CREATED);
}
If you want to know correct https statuses for a POST method I suggest to read this: https://stackoverflow.com/questions/43327963/what-status-code-should-we-return-if-the-post-succeeds-but-does-not-result-in-cr
答案3
得分: 1
首先,用于测试HTTP控制器的方法是使用MockMvc
。
示例测试:
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"id\": 1, \"Name\": \"John\", \"Age\": 22 }")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.id").value($id))
.andExpect(jsonPath("$.value").value("some-value"));
更多详细信息请查看文档。
要返回201状态码,您需要在您的控制器中添加@ResponseStatus(HttpStatus.CREATED)
。
英文:
First of all, for testing HTTP controller You can use MockMvc
.
Examples test:
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{ "id": 1, "Name": "John", "Age": 22 }")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.id").value($id))
.andExpect(jsonPath("$.value").value("some-value"));
More details in docs
To return 201 code, You need to add @ResponseStatus(HttpStatus.CREATED)
in Your controller.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论