package me.weekbelt.studyolle.study;
@RequiredArgsConstructor
@Transactional
@SpringBootTest
@AutoConfigureMockMvc
class StudyControllerTest {
@Autowired
MockMvc mockMvc;
@Autowired
StudyService studyService;
@Autowired
StudyRepository studyRepository;
@Autowired
AccountRepository accountRepository;
@AfterEach
void afterEach() {
accountRepository.deleteAll();
}
@Test
@WithAccount("joohyuk")
@DisplayName("스터디 개설 폼 조회")
public void createStudyForm() throws Exception {
mockMvc.perform(get("/new-study"))
.andExpect(view().name("study/form"))
.andExpect(model().attributeExists("account"))
.andExpect(model().attributeExists("studyForm"));
}
@Test
@WithAccount("joohyuk")
@DisplayName("스터디 개설 - 완료")
public void createStudy_success() throws Exception {
mockMvc.perform(post("/new-study")
.param("path", "test-path")
.param("title", "study title")
.param("shortDescription", "short description of a study")
.param("fullDescription", "full description of a study")
.with(csrf()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/study/test-path"));
Study study = studyRepository.findByPath("test-path");
assertThat(study).isNotNull();
Account account = accountRepository.findByNickname("joohyuk");
assertThat(study.getManagers().contains(account));
}
@Test
@WithAccount("joohyuk")
@DisplayName("스터디 개설 - 실패")
public void createStudy_fail() throws Exception {
Study study = new Study();
study.setPath("test-path");
study.setTitle("test study");
study.setShortDescription("short description");
study.setFullDescription("<p>full description</p>");
Account joohyuk = accountRepository.findByNickname("joohyuk");
studyService.createNewStudy(study, joohyuk);
mockMvc.perform(post("/new-study")
.param("path", "test-path")
.param("title", "study title")
.param("shortDescription", "short description of a study")
.param("fullDescription", "full description of a study")
.with(csrf()))
.andExpect(status().isOk())
.andExpect(view().name("study/form"))
.andExpect(model().hasErrors())
.andExpect(model().attributeExists("studyForm"))
.andExpect(model().attributeExists("account"));
}
@Test
@WithAccount("joohyuk")
@DisplayName("스터디 조회")
public void viewStudy() throws Exception {
Study study = new Study();
study.setPath("test-path");
study.setTitle("test study");
study.setShortDescription("short description");
study.setFullDescription("<p>full description</p>");
Account joohyuk = accountRepository.findByNickname("joohyuk");
studyService.createNewStudy(study, joohyuk);
mockMvc.perform(get("/study/test-path"))
.andExpect(view().name("study/view"))
.andExpect(model().attributeExists("account"))
.andExpect(model().attributeExists("study"));
}
}
참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#
'스프링과 JPA 기반 웹 어플리케이션 개발 > 4부 스터디' 카테고리의 다른 글
53. 스터디 설정 - 배너 (0) | 2020.04.24 |
---|---|
52. 스터디 설정 - 소개 수정 (0) | 2020.04.24 |
50. 스터디 구성원 조회 (0) | 2020.04.23 |
49. 스터디 조회 (0) | 2020.04.23 |
48. 스터디 개설 (0) | 2020.04.23 |