Junit4를 사용하기 위해 의존성에 spring-boot-starter-test를 추가해줍니다.
testCompile("org.springframework.boot:spring-boot-starter-test")
@RestController
@RequestMapping("/mockmvc")
public class MockMvcController {
@GetMapping("")
public String mockGet(@RequestParam String name, @RequestParam String id){
return name + "의 MockMvc 테스트입니다. " + id;
}
}
@WebMvcTest(MockMvcController.class)
class MockMvcControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test_get() throws Exception {
MultiValueMap<String, String> info = new LinkedMultiValueMap<>();
info.add("name", "shbae");
info.add("id", "shbae94");
mockMvc.perform(get("/mockmvc")
.params(info))
.andExpect(status().isOk())
.andExpect(content().string("shbae의 MockMvc 테스트입니다. shbae94"))
.andDo(print());
}
}
MockHttpServletRequest:
HTTP Method = GET
Request URI = /mockmvc
Parameters = {name=[shbae], id=[shbae94]}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.fastcampus.mock.controller.MockMvcController
Method = com.fastcampus.mock.controller.MockMvcController#mockGet(String, String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"text/plain;charset=UTF-8", Content-Length:"44"]
Content type = text/plain;charset=UTF-8
Body = shbae의 MockMvc 테스트입니다. shbae94
Forwarded URL = null
Redirected URL = null
Cookies = []
BUILD SUCCESSFUL in 28s
1) perform()
2) get("/mockmvc")
3) params(info)
4) andExpect()
5) andDo(print())
public class Info{
private String name;
private String id;
public Info(String name, String id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public String getId(){
return id;
}
}
@RestController
@RequestMapping("mock")
public class MockController {
@PostMapping
public String MockPost(@RequestBody Info info){
return info.getName()+"님의 테스트입니다. " + info.getId();
}
}
@WebMvcTest(MockController.class)
class MockControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WebApplicationContext ctx;
@BeforeEach
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(ctx)
.addFilters(new CharacterEncodingFilter("UTF-8", true)) // 한글 깨짐 처리
.build();
}
@Test
public void test_post() throws Exception{
String param = objectMapper.writeValueAsString(new Info("shbae", "shbaeTest"));
mockMvc.perform(post("/mock")
.content(param)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("shbae님의 테스트입니다. shbaeTest"))
.andDo(print());
}
}
MockHttpServletRequest:
HTTP Method = POST
Request URI = /mock
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"33"]
Body = {"name":"shbae","id":"shbaeTest"}
Session Attrs = {}
Handler:
Type = com.fastcampus.mockmvc.controller.MockController
Method = com.fastcampus.mockmvc.controller.MockController#MockPost(Info)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"41"]
Content type = application/json;charset=UTF-8
Body = shbae님의 테스트입니다. shbaeTest
Forwarded URL = null
Redirected URL = null
Cookies = []
BUILD SUCCESSFUL in 11s
4 actionable tasks: 2 executed, 2 up-to-date
오후 7:09:41: Task execution finished ':test --tests "com.fastcampus.mockmvc.controller.MockControllerTest.test_post"'.