폼 로그인 관련 기능을 더 살펴보자.
@Test
public void login() throws Exception {
mockMvc.perform(formLogin().user("cmlee").password("123"))
.andDo(print())
.andExpect(authenticated());
}
@Test
public void login() throws Exception {
Account user = this.createUser();
mockMvc.perform(formLogin().user(user.getUsername()).password("123"))
.andDo(print())
.andExpect(authenticated());
}
public Account createUser() {
Account account = new Account();
account.setUsername("cmlee");
account.setPassword("123");
account.setRole("USER");
return accountService.createAccount(account);
}
테스트에서 디비를 건드릴 때는 @Transactional을 추가해 주는 것이 좋다. 만약 똑같은 유저를 각각의 테스트에서 insert할 때는 error가 발생한다. unique조건에 맞지 않기 때문에
이 경우 @Transactional을 붙이면 각각의 테스트가 끝나고 롤백이 된다.
이제부터가 시작입니다.