-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24ab9d4
commit 624c71d
Showing
6 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Hello_Spring_Boot/src/main/java/Spring_Boot_Study/Hello_Spring_Boot/domain/Member.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.domain; | ||
|
||
public class Member { | ||
private Long id; | ||
private String name; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...g_Boot/src/main/java/Spring_Boot_Study/Hello_Spring_Boot/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.repository; | ||
|
||
import Spring_Boot_Study.Hello_Spring_Boot.domain.Member; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface MemberRepository { | ||
Member save(Member member); // 회원 정보 저장 | ||
Optional<Member> findById(Long id); | ||
Optional<Member> findByName(String name); | ||
List<Member> findAll(); | ||
} |
43 changes: 43 additions & 0 deletions
43
.../src/main/java/Spring_Boot_Study/Hello_Spring_Boot/repository/MemoryMemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.repository; | ||
|
||
import Spring_Boot_Study.Hello_Spring_Boot.domain.Member; | ||
|
||
import java.util.*; | ||
|
||
public class MemoryMemberRepository implements MemberRepository { | ||
|
||
private static Map<Long, Member> store = new HashMap<>(); | ||
private static long sequence = 0L; | ||
|
||
@Override | ||
public Member save(Member member) { | ||
member.setId(++sequence); | ||
store.put(member.getId(), member); | ||
return member; | ||
} | ||
|
||
@Override | ||
public Optional<Member> findById(Long id) { | ||
// null이 리턴될 가능성이 있을땐 Optional사용. 이렇게하면 null 대신 optional이 감싸짐. | ||
return Optional.ofNullable(store.get(id)); | ||
} | ||
|
||
@Override | ||
public Optional<Member> findByName(String name) { | ||
return store.values().stream() | ||
.filter(member -> member.getName().equals(name)) | ||
.findAny(); | ||
// 자바 람다식 | ||
// findAny(): 하나라도 찾는 것 | ||
} | ||
|
||
@Override | ||
public List<Member> findAll() { | ||
return new ArrayList<>(store.values()); | ||
} | ||
|
||
// repository 비워줌(테스트 할 때 사용) | ||
public void clearStore(){ | ||
store.clear(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
..._Spring_Boot/src/main/java/Spring_Boot_Study/Hello_Spring_Boot/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.service; | ||
|
||
import Spring_Boot_Study.Hello_Spring_Boot.domain.Member; | ||
import Spring_Boot_Study.Hello_Spring_Boot.repository.MemberRepository; | ||
import Spring_Boot_Study.Hello_Spring_Boot.repository.MemoryMemberRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class MemberService { | ||
// 기존의 new 로 새 객체를 생성하는 대신 constructor로 외부에서 넣어주도록 변경 | ||
// DI(Dependency Injection) - 외부에서 넣어줌 | ||
private final MemberRepository memberRepository; | ||
|
||
public MemberService(MemberRepository memberRepository) { | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
/** | ||
* 회원가입 | ||
*/ | ||
public Long join(Member member) { | ||
validateDuplicateMember(member); //중복 회원 검증 | ||
memberRepository.save(member); | ||
return member.getId(); | ||
} | ||
private void validateDuplicateMember(Member member) { | ||
// 여기서는 중복 이름 안되게 설정 | ||
|
||
// "memberRepository.findByName((member.getName()));" 까지만 쓰고 | ||
// ctrl + alt + v 하면 자동으로 할당 받는 변수 생성해줌. | ||
// Optional<Member> result = memberRepository.findByName((member.getName())); | ||
// result.ifPresent(m -> { | ||
// throw new IllegalStateException("이미 존재하는 회원입니다."); | ||
// }); | ||
|
||
// 위 표현 대신 아래처럼 하느게 이쁨. | ||
// memberRepository.findByName(member.getName()) | ||
// .ifPresent(m -> { | ||
// throw new IllegalStateException("이미 존재하는 회원입니다."); | ||
// }); | ||
|
||
// 위의 것을 메소드로 지정 | ||
validateDuiplicateMember(member); | ||
} | ||
|
||
private void validateDuiplicateMember(Member member) { | ||
memberRepository.findByName(member.getName()) | ||
.ifPresent(m -> { | ||
throw new IllegalStateException("이미 존재하는 회원입니다."); | ||
}); | ||
} | ||
|
||
/** | ||
* 전체 회원 조회 | ||
*/ | ||
public List<Member> findMembers() { | ||
return memberRepository.findAll(); | ||
} | ||
|
||
public Optional<Member> findOne(Long memberId) { | ||
return memberRepository.findById(memberId); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
.../test/java/Spring_Boot_Study/Hello_Spring_Boot/repository/MemoryMemberRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.repository; | ||
|
||
import Spring_Boot_Study.Hello_Spring_Boot.domain.Member; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
class MemoryMemberRepositoryTest { | ||
|
||
MemoryMemberRepository repository = new MemoryMemberRepository(); | ||
|
||
// 콜백 메소드 | ||
@AfterEach | ||
public void afterEach() { | ||
// 테스트 하나 끝나면 repository 지워줌 | ||
repository.clearStore(); | ||
} | ||
|
||
//실행 단축키: ctrl + shift + F10 | ||
@Test | ||
public void save() { | ||
//given | ||
Member member = new Member(); | ||
member.setName("spring"); | ||
//when | ||
repository.save(member); | ||
//then | ||
// optional에서 값을 꺼낼 땐 get()이용(사실 get이 좋은 방법은 아닌데 테스트이므로) | ||
Member result = repository.findById(member.getId()).get(); | ||
|
||
// 검증 | ||
|
||
// 위에 new 만든 값이랑 메모리에 저장한 값이 같은지 확인 | ||
//성공하면 파란불. 아니면 빨간 메세지 | ||
//Assertions.assertEquals(member, result); | ||
|
||
// 요즘 많이 쓰는 방법 | ||
assertThat(member).isEqualTo(result); | ||
//Assertions.assertThat(member).isEqualTo(result); | ||
// alt+enter -> static import하면 'Assertions' 숨기고 앞으로는 assertThat만 써도 됨 | ||
} | ||
|
||
@Test | ||
public void findByName() { | ||
//given | ||
Member member1 = new Member(); | ||
member1.setName("spring1"); | ||
repository.save(member1); | ||
|
||
// 팁: 위에 member1 세팅 세줄 복붙하고 shift+f6 누르고 이름을 "member2"로 바꾸면 | ||
// 나머지도 member로 따라 같이 바뀜. | ||
Member member2 = new Member(); | ||
member2.setName("spring2"); | ||
repository.save(member2); | ||
//when | ||
Member result = repository.findByName("spring1").get(); | ||
// get() 맨뒤에 쓰면 optional 꺼내서 줌. | ||
|
||
//then | ||
assertThat(result).isEqualTo(member1); | ||
} | ||
|
||
@Test | ||
public void findAll() { | ||
//given | ||
Member member1 = new Member(); | ||
member1.setName("spring1"); | ||
repository.save(member1); | ||
Member member2 = new Member(); | ||
member2.setName("spring2"); | ||
repository.save(member2); | ||
//when | ||
List<Member> result = repository.findAll(); | ||
//then | ||
assertThat(result.size()).isEqualTo(2); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...ing_Boot/src/test/java/Spring_Boot_Study/Hello_Spring_Boot/service/MemberServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package Spring_Boot_Study.Hello_Spring_Boot.service; | ||
|
||
import Spring_Boot_Study.Hello_Spring_Boot.domain.Member; | ||
import Spring_Boot_Study.Hello_Spring_Boot.repository.MemoryMemberRepository; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class MemberServiceTest { | ||
|
||
//MemberService memberService = new MemberService(); | ||
//MemoryMemberRepository memberRepository = new MemoryMemberRepository(); | ||
|
||
// 위 방식은 각자 다른 객체를 생성하므로 좋지 않음. | ||
// 현재는 store가 static 방식이므로 문제될 건 없지만 아래처럼 하는게 좋음. | ||
MemberService memberService; | ||
MemoryMemberRepository memberRepository; | ||
|
||
// 각 테스트 실행 전에 이거 먼저 실행 | ||
@BeforeEach | ||
public void beforeEach() { | ||
// 테스트 실행할 때마다 각각 생성 | ||
memberRepository = new MemoryMemberRepository(); | ||
memberService = new MemberService(memberRepository); | ||
} | ||
|
||
// 콜백 메소드 | ||
@AfterEach | ||
public void afterEach() { | ||
// 테스트 하나 끝나면 memberRepository 지워줌 | ||
memberRepository.clearStore(); | ||
} | ||
|
||
@Test | ||
void 회원가입() { | ||
//Given | ||
Member member = new Member(); | ||
member.setName("hello"); | ||
|
||
//When | ||
Long saveId = memberService.join(member); | ||
|
||
//Then | ||
|
||
Member findMember = memberService.findOne(saveId).get(); | ||
Assertions.assertThat(member.getName()).isEqualTo(findMember.getName()); | ||
|
||
//assertEquals(member.getName(), findMember.getName()); | ||
} | ||
|
||
@Test | ||
public void 중복_회원_예외(){ | ||
//Given | ||
// member1과 member2 이름이 같음. | ||
Member member1 = new Member(); | ||
member1.setName("spring"); | ||
Member member2 = new Member(); | ||
member2.setName("spring"); | ||
|
||
//When | ||
memberService.join(member1); | ||
|
||
// 예외가 제대로 발생하는지 확인 | ||
|
||
// try { | ||
// memberService.join(member2); | ||
// fail(); | ||
// } catch(IllegalStateException e){ | ||
// assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다."); | ||
// } | ||
|
||
// 위 방법 대신 아래 방법 사용 | ||
assertThrows(IllegalStateException.class, () -> memberService.join(member2)); | ||
|
||
// 메시지 검증할 때 | ||
IllegalStateException e = assertThrows(IllegalStateException.class, | ||
() -> memberService.join(member2));//예외가 발생해야 한다. | ||
assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다."); | ||
} | ||
|
||
|
||
@Test | ||
void findMembers() { | ||
} | ||
|
||
@Test | ||
void findOne() { | ||
} | ||
} |