반응형
1. 기존 MemberServiceTest크래스를 복사하여 MemberServiceIntegrationTest 클래스를 생성 후 아래와 같이 코드를 작성합니다.
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.MemoryMemberRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class MemberServiceIntegrationTest {
@Autowired MemberService memberService;
@Autowired MemberRepository memberRepository;
@Test
void 회원가입() {
//given
Member member = new Member();
member.setName("hello");
//when
Long saveId = memberService.join(member);
//then
Member findMember = memberService.findOne(saveId).get();
assertThat(member.getName()).isEqualTo(findMember.getName());
}
@Test
public void 중복_회원_예외(){
//given
Member member1 = new Member();
member1.setName("spring");
Member member2 = new Member();
member2.setName("spring");
//when
memberService.join(member1);
IllegalStateException e = assertThrows(IllegalStateException.class, () -> memberService.join(member2));
assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다.");
//then
}
}
2. H2 데이터베이스에 기존 작성한 spring 네임이 존재할 경우 에러가 나니, 데이터베이스 현황을 확인 후 테스트하시기 바랍니다.
반응형
'Programming > Spring' 카테고리의 다른 글
Spring boot 시작하기 - 14강(스프링데이터JPA) (0) | 2020.11.30 |
---|---|
Spring boot 시작하기 - 13강(JPA) (0) | 2020.11.30 |
Spring boot 시작하기 - 11강(H2 Database연결-jdbc template방식) (0) | 2020.11.30 |
Spring boot 시작하기 - 10강(H2 Database생성) (0) | 2020.11.29 |
Spring boot 시작하기 - 9강(조회) (0) | 2020.11.27 |