#14 simple blog : create post api
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.example.simpleblog.controller;
|
||||
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class PostController {
|
||||
|
||||
@PostMapping("/posts")
|
||||
public String post(@RequestBody PostCreate params) {
|
||||
log.info("params={}", params);
|
||||
return "Hello World";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.simpleblog.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
public class PostCreate {
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.simpleblog.controller;
|
||||
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest
|
||||
class PostControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
@DisplayName("/posts 요청시 Hello World를 출력한다.")
|
||||
void test() throws Exception {
|
||||
PostCreate request = new PostCreate("글 제목입니다.", "글 내용입니다.");
|
||||
String jsonString = objectMapper.writeValueAsString(request);
|
||||
|
||||
// expected
|
||||
mockMvc.perform(post("/posts")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(jsonString)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Hello World"))
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user