@ActiveProfiles 어노테이션은 테스트시 지정된 프로파일로 테스트를 진행할 수 있도록 설정한다.
@ActiveProfiles("test") // 단일 프로파일
@ActiveProfiles({"test", "local"}) // 복수 프로파일
MockMvc는 스프링 MVC 테스트를 위한 핵심적인 클래스이다.
@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureMockMvc
@Transactional
public class MemberControllerTest{
@Autowired
private MemberService memberService;
@Autowired
private MockMvc mvc;
@Test
void joinTest() throws Exception {
ResultActions resultActions = mvc
.perform(
post("/members/join")
.content("""
{
"username": "test",
"password": "1234",
"nickname": "test"
}
""".stripIndent())
.contentType(
new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8)
)
)
.andDo(print());
resultActions
.andExpect(handler().handlerType(MemberController.class))
.andExpect(handler().methodName("join"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.resultCode").value("201-1"))
.andExpect(jsonPath("$.message").value("test님 환영합니다."))