@SpringBootTest
@Transactional
public class MemberServiceIntegrationTest {
@Autowired MemberService memberService;
@Autowired Repository repository;
@Test
void join() {
//given
Member member = new Member();
member.setName("goat1");
//when
Long saveId = memberService.join(member);
//then
Member findMember = memberService.findOne(member.getId()).get(); //Optional로 반환되는거 get으로 member를 꺼냄
Assertions.assertThat(member.getName()).isEqualTo(findMember.getName());
}
@Test
void 중복_회원_예외(){
//given
Member member1 = new Member();
Member member2 = new Member();
member1.setName("goat1");
member2.setName("goat1");
//when
memberService.join(member1);
IllegalStateException msg = assertThrows(IllegalStateException.class, () -> memberService.join(member2));
Assertions.assertThat(msg.getMessage()).isEqualTo("이미 존재하는 회원입니다.");
// try{
// memberService.join(member2);
// fail();
// }catch(IllegalStateException e ){
// Assertions.assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다.1");
// }
//then
}
@Test
void findMembers() {
}
@Test
void findOne() {
}
}
Memory를 활용한 Repository에서 @aftereach, @beforeeach를 활용하여 반복적인 test를 할 수 있었지만,
스프링 부트는 하지 않아도 된다.
이는 @Transactional 어노테이션 덕분이다. ->DB에 rollback을 하는 역할