https://github.com/jiholee0/hello-spring
1. ๋น์ฆ๋์ค ์๊ตฌ์ฌํญ ์ ๋ฆฌ
2. ํ์ ๋๋ฉ์ธ๊ณผ ๋ฆฌํฌ์งํ ๋ฆฌ ๋ง๋ค๊ธฐ
3. ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ํ ์คํธ ์ผ์ด์ค ์์ฑ
4. ํ์ ์๋น์ค ๊ฐ๋ฐ
5. ํ์ ์๋น์ค ํ ์คํธ
1. ๋น์ฆ๋์ค ์๊ตฌ์ฌํญ ์ ๋ฆฌ
- ๋ฐ์ดํฐ : ํ์ ID, ์ด๋ฆ
- ๊ธฐ๋ฅ : ํ์ ๋ฑ๋ก, ์กฐํ
์น ์ ํ๋ฆฌ์ผ์ด์ ๊ณ์ธต ๊ตฌ์กฐ
- ์ปจํธ๋กค๋ฌ : ์น MVC์ ์ปจํธ๋กค๋ฌ ์ญํ
- ์๋น์ค : ํต์ฌ ๋น์ฆ๋์ค ๋ก์ง ๊ตฌํ
- ๋ฆฌํฌ์งํ ๋ฆฌ : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผ, ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ DB์ ์ ์ฅํ๊ณ ๊ด๋ฆฌ
- ๋๋ฉ์ธ : ๋น์ฆ๋์ค ๋๋ฉ์ธ ๊ฐ์ฒด, ์) ํ์, ์ฃผ๋ฌธ, ์ฟ ํฐ ๋ฑ ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ณ ๊ด๋ฆฌ
2. ํ์ ๋๋ฉ์ธ๊ณผ ๋ฆฌํฌ์งํ ๋ฆฌ ๋ง๋ค๊ธฐ
ํ์ ๊ฐ์ฒด์ ๋ฆฌํฌ์งํ ๋ฆฌ ์ธํฐํ์ด์ค, ๋ฉ๋ชจ๋ฆฌ ๊ตฌํ์ฒด๋ฅผ ๊ฐ๋ฐํ์๋ค.
ํ์ ๊ฐ์ฒด (/domain/Member.java)
package hello.hellospring.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;
}
}
ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ์ธํฐํ์ด์ค (/repository/MemberRepository.java)
package hello.hellospring.repository;
import hello.hellospring.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();
}
ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ ๊ตฌํ์ฒด (/repository/MemoryMemberRepository.java
package hello.hellospring.repository;
import hello.hellospring.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) {
return Optional.ofNullable(store.get(id));
}
@Override
public Optional<Member> findByName(String name) {
return store.values().stream()
.filter(member -> member.getName().equals(name))
.findAny();
}
@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
}
public void clearStore(){
store.clear();
}
}
3. ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ํ ์คํธ ์ผ์ด์ค ์์ฑ
๊ฐ๋ฐํ ๊ธฐ๋ฅ์ ์คํํด์ ํ ์คํธ ํ ๋ ์๋ฐ์ main ๋ฉ์๋๋ฅผ ํตํด์ ์คํํ๊ฑฐ๋, ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ปจํธ๋กค๋ฌ๋ฅผ ํตํด์ ํด๋น ๊ธฐ๋ฅ์ ์คํํ๋ค. ์ด๋ฌํ ๋ฐฉ๋ฒ์ ์ค๋นํ๊ณ ์คํํ๋๋ฐ ์ค๋ ๊ฑธ๋ฆฌ๊ณ , ๋ฐ๋ณต ์คํํ๊ธฐ ์ด๋ ต๊ณ ์ฌ๋ฌ ํ ์คํธ๋ฅผ ํ๋ฒ์ ์คํํ๊ธฐ ์ด๋ ต๋ค๋ ๋จ์ ์ด ์๋ค. ์๋ฐ๋ JUnit์ด๋ผ๋ ํ๋ ์์ํฌ๋ก ํ ์คํธ๋ฅผ ์คํํด์ ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค
ํ ์คํธ ์์์ ์์กด๊ด๊ณ๊ฐ ์์ด์ผ ํจ์ ์ ์ํ์ฌ์ผ ํ๋ค. ํ๋ฒ์ ์ฌ๋ฌ ํ ์คํธ๋ฅผ ์คํํ๋ฉด ๋ฉ๋ชจ๋ฆฌ DB์ ์ง์ ํ ์คํธ์ ๊ฒฐ๊ณผ๊ฐ ๋จ์ ์ ์๋ค. ์ด๋ ๊ฒ ๋๋ฉด ๋ค์ ์ด์ ํ ์คํธ ๋๋ฌธ์ ๋ค์ ํ ์คํธ๊ฐ ์คํจํ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฏ๋ก, @AfterEach ๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ ํ ์คํธ๊ฐ ์ข ๋ฃ๋ ๋ ๋ง๋ค ๋ฉ๋ชจ๋ฆฌ DB์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ฒ๋ ํ์๋ค.
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.*;
public class MemoryMemberRepositoryTest {
MemoryMemberRepository repository = new MemoryMemberRepository();
@AfterEach
public void afterEach() {
repository.clearStore();
}
@Test
public void save() {
Member member = new Member();
member.setName("spring");
repository.save(member);
Member result = repository.findById(member.getId()).get();
// Assertions.assertEquals(member, result);
assertThat(member).isEqualTo(result);
}
@Test
public void findByName(){
Member member1 = new Member();
member1.setName("spring1");
repository.save(member1);
Member member2 = new Member();
member2.setName("spring2");
repository.save(member2);
Member result = repository.findByName("spring1").get();
assertThat(result).isEqualTo(member1);
}
@Test
public void findAll(){
Member member = new Member();
member.setName("spring");
repository.save(member);
List<Member> result = repository.findAll();
assertThat(result.size()).isEqualTo(1);
}
}
4. ํ์ ์๋น์ค ๊ฐ๋ฐ
๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๋ ์๋น์ค ์ฝ๋์ด๋ค. ๋จ์ํ ํ์ ๊ฐ์ ๋ฐ ํ์ ์กฐํ๋ง ๋ง๋ค์๋ค.
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberRepository;
import java.util.List;
import java.util.Optional;
public class MemberService {
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())
.ifPresent(m -> {
throw new IllegalStateException("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฆ์
๋๋ค.");
});
}
/**
* ์ ์ฒด ํ์ ์กฐํ
*/
public List<Member> findMembers() {
return memberRepository.findAll();
}
public Optional<Member> findOne(Long memberId) {
return memberRepository.findById(memberId);
}
}
5. ํ์ ์๋น์ค ํ ์คํธ
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemoryMemberRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
class MemberServiceTest {
MemberService memberService;
MemoryMemberRepository memberRepository;
@BeforeEach
public void beforeEach() {
memberRepository = new MemoryMemberRepository();
memberService = new MemberService(memberRepository);
}
@AfterEach
public void afterEach() {
memberRepository.clearStore();
}
@Test
void ํ์๊ฐ์
() {
// given
Member member = new Member();
member.setName("spring");
// when
Long saveId = memberService.join(member);
// then
Member findMember = memberService.findOne(saveId).get();
assertThat(member.getName()).isEqualTo(findMember.getName());
}
@Test
void ํ์๊ฐ์
_์ค๋ณต_ํ์_์์ธ() {
Member member = new Member();
member.setName("spring");
Member memberSameName = new Member();
memberSameName.setName("spring");
Long saveId = memberService.join(member);
IllegalStateException e = assertThrows(IllegalStateException.class, () -> memberService.join(memberSameName));
assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฆ์
๋๋ค.");
// try {
// Long saveId2 = memberService.join(memberSameName);
// fail();
// } catch (IllegalStateException e) {
// assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฆ์
๋๋ค.");
// }
}