MemberServiceTest를 실행하면 에러가 뜹니다.
package com.boardproject.service; import com.boardproject.jpashop.domain.Member; import com.boardproject.repository.MemberRepository; import com.boardproject.service.MemberService; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.Extension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional // pk값이 같으면 같은 영속성으로 관리한다. public class MemberServiceTest { @Autowired MemberService memberService; @Autowired MemberRepository memberRepository; @Test public void 회원가입() throws Exception{ //given Member member = new Member(); member.setName("jeong"); //when Long savedId = memberService.join(member); //then assertEquals(member, memberRepository.findOne(savedId)); } @Test public void 중복회원예외() throws Exception{ //given Member member1 = new Member(); member1.setName("jeong"); Member member2 = new Member(); member1.setName("jeong"); //when memberService.join(member1); assertThrows(IllegalAccessError.class, () -> memberService.join(member2)); //then } } (사진) 두번째 질문입니다. MemberService 예외처리할때 영상을 그대로 따라하니까 에러 뜨는데, 이렇게 작성해도 맞는지 확인 부탁드립니다.package com.boardproject.service; import com.boardproject.jpashop.domain.Member; import com.boardproject.repository.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional(readOnly = true) // JPA가 조회할때 최적화한다, 읽기엔 이거 넣는게 좋다 @RequiredArgsConstructor //final 가진 애들의 생성자를 자동 생성해준다 public class MemberService { private final MemberRepository memberRepository; //컴파일 시점에 값세팅 확인할수 있어 final 설정 //회원 가입 @Transactional //읽기가 아니라서 readonly 무시하기 위해 썼음 public Long join(Member member) throws IllegalAccessException { validaateDuplicateMember(member); //중복회원 검증 memberRepository.save(member); return member.getId(); } private void validaateDuplicateMember(Member member) throws IllegalAccessException { //Exception List findMembers = memberRepository.findByName(member.getName()); if(!findMembers.isEmpty()){ throw new IllegalAccessException("이미 존재하는 회원입니다"); } } //회원 전체 조회 public List findMembers(){ return memberRepository.findAll(); } public Member findOne(Long id){ return memberRepository.findOne(id); } }