스프링부트 테스트코드 작성 - MVC

jhstudio·2022년 11월 8일
0

MVC로 할 수 있는 테스트코드를 각종 경우의수로 적는 글

HTTP 요청

@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());
	}

}
profile
잡부

0개의 댓글