항해 31일차 2022.02.09
테스트 코드에 @WebMvcTest를 추가해 사용할 수 있다.
예제
@WebMvcTest( controllers = {UserController.class, ProductController.class}, excludeFilters = { @ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfig.class ) } ) class UserProductMvcTest { private MockMvc mvc;
private Principal mockPrincipal;
@Autowired
private WebApplicationContext context;
@Autowired
private ObjectMapper objectMapper;
@MockBean
UserService userService;
...
@Test
@DisplayName("회원 가입 요청 처리")
void test2() throws Exception {
// given
MultiValueMap<String, String> signupRequestForm = new LinkedMultiValueMap<>();
signupRequestForm.add("username", "제이홉");
signupRequestForm.add("password", "hope!@#");
signupRequestForm.add("email", "hope@sparta.com");
signupRequestForm.add("admin", "false");
// when - then
mvc.perform(post("/user/signup")
.params(signupRequestForm)
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/user/login"))
.andDo(print());
}