MVC로 할 수 있는 테스트코드를 각종 경우의수로 적는 글
@SpringBootTest
@AutoConfigureMockMvc
class CustomControllerTest {
@Autowired
MockMvc mvc;
@Test
@DisplayName("로그인 기능")
void testLogin() throws Exception {
// given
String id = "id";
String password = "password!";
CustomVO customVO = new CustomVO();
customVO.setCustomPassword(password);
customVO.setCustomId(id);
String json = new Gson().toJson(customVO);
String token = "jwt";
// when
final ResultActions actions = mvc.perform(post("/custom/login")
.content(json) // body
.header("Authorization", token) // header
.characterEncoding("UTF-8")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON));
// then
actions.andDo(print()).andExpect(status().isOk());
}
}