์ปจํธ๋กค๋ฌ : ์น MVC์ ์ปจํธ๋กค๋ฌ ์ญํ
์๋น์ค : ํต์ฌ ๋น์ฆ๋์ค ๋ก์ง ๊ตฌํ
๋ฆฌํฌ์งํ ๋ฆฌ : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผ, ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ DB์ ์ ์ฅํ๊ณ ๊ด๋ฆฌ
๋๋ฉ์ธ : ๋น์ฆ๋์ค ๋๋ฉ์ธ ๊ฐ์ฒด ex) ํ์, ์ฃผ๋ฌธ, ์ฟ ํฐ ๋ฑ ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ณ ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด
๋ ํฌ์งํ ๋ฆฌ๋ฅผ ์ธํฐํ์ด์ค๋ก ์ค์ ํ ์ด์ : DB์ ๋ํ ์ ํ์ ์์ง ํ์ง ์์ DB์ ๋ฐ๋ผ ๊ตฌํ์ฒด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ ์ธํฐํ์ด์ค๋ก ๋ง๋ ๋ค.
์ค์ต ์๊ตฌ์ฌํญ
๋ฐ์ดํฐ : ํ์ ID, ์ด๋ฆ
๊ธฐ๋ฅ : ํ์ ๋ฑ๋ก, ์กฐํ
์์ง ๋ฐ์ดํฐ ์ ์ฅ์๊ฐ ์ ์ ๋์ง ์์ ์์
๋๋ฉ์ธ๊ณผ ๋ ํฌ์งํ ๋ฆฌ ๋ง๋ค๊ธฐ
1) Domian - Member
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;
}
}
2) MemberRepository
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();
}
3) MemoryMemerRepository
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); // id๊ฐ์ ์ฆ๊ฐ
store.put(member.getId(), member); // store์ ์ ์ฅ
return member;
}
@Override
public Optional<Member> findById(Long id) {
return Optional.ofNullable(store.get(id));
// Optional ~ Null๊ฐ์ด์ด๋ Optional ํ์
์ผ๋ก ๋ฐํํ ์ ์๊ณ , ๊ทธ ๊ฒ์ ๋ฐํ์ผ๋ก
// ์ฌ์ฉ์์ ์์
์ด ๊ฐ๋ฅํ๋ค
}
@Override
public Optional<Member> findByname(String name) {
return store.values().stream()
.filter(member -> member.getName().equals(name))
// ๋ฉค๋ฒ์ ์ ์ฅ๋ ์ด๋ฆ์ด ํ๋ผ๋ฏธํฐ์ ๋ค์ด์จ ์ด๋ฆ๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ง filtering
.findAny();
// ํ๋๋ผ๋ ๊ฐ์ ์ฐพ์ผ๋ฉด Optional ํ์
์ ์ ์ฅํ์ฌ ๋ฐํ, ํ๋๋ ์๋ค๋ฉด null์ ๋ด์ ๋ฐํ
}
@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
// store.values ~ member๋ฅผ ๋ฆฌ์คํธ์ ์ ์ฅํ๊ณ ๊ทธ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ ํ์
}
public void clearStore() {
store.clear();
}
}
ํ์ ๋ ํฌ์งํ ๋ฆฌ ํ ์คํธ ์ผ์ด์ค ์์ฑ
Junit ํ๋ ์์ํฌ ์ฌ์ฉ
Assertions ํด๋์ค์ assert๋ฌธ์ ํตํด ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค
*๋ชจ๋ ํ ์คํธ ๋ฉ์๋๋ ์์์ ์๊ด์์ด ์ ์๋ํด์ผํ๋ค(์๋ก ๊ฐ ์์์ ์์กด์ฑ์ด ์์ด์ผ ํ๋ค)
-> ํ ์คํธ๊ฐ ๋๋๊ณ ๋๋ฉด Clear๋ฅผ ํด์ผํ๋ค : @AfterEach ํด๋ฆฌ์ด ๋งค์๋ ์์ฑํ ๊ฒ ~ ํ ์คํธ ์ฝ๋, ๋ณธ ์ฝ๋ ๋ชจ๋
pakage hello.hellospring.repository;
import hello.hellospring.domain.Member;
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.*;
class MemoryMemberRepositoryTest {
MemoryMemberRepository repository = new MemoryMemberRepository();
@AfterEach
public void afterEach() {
repository.clearStore();
}
@Test
public void save() {
// given
Member member = new Member();
member.setName("spring");
// when
repository.save(member);
// then
Member result = repository.findById(member.getId()).get();
assertThat(result).isEqualTo(member);
}
@Test
public void findByName() {
// given
Member member1 = new Member();
member1.setName("spring1");
repository.save(member1);
Member member2 = new Member();
memeber2.setName("spring2");
repository.save(member2);
// when
Member result = repository.findByName("spring1").get();
// then
assertThat(result).isEquals(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);
}
}
ํ์ ์๋น์ค ๊ฐ๋ฐ
์ต์ ๋ ํด๋์ค : ๋ค์ํ ํ์ ์ ์ต์ ๋ ํด๋์ค๋ก ํ๋ฒ ๊ฐ์๊ธฐ ๋๋ฌธ์, ์ต์ ๋ ํด๋์ค์ ๋ค์ํ ๋ฉ์๋๋ฅผ ์ฌ์ฉ ๊ฐ๋ฅ
ex) ifPresent(x-> { throw new IllegalArgumentException("์ด๋ฏธ ์กด์ฌํ๋ ํ์์ ๋๋ค");}); ~ ์์ธ์ฒ๋ฆฌ
+ get().orElseGet()๋ ๋ง์ด ์ฌ์ฉํ๋ค
-> ๋ฉ์๋์ ๊ธฐ๋ฅ์ ๊ฐ๋ฅํ ์ถ์์ํจ๋ค ~ ๋ค๋ฅธ ๋ฉ์๋๋ก ๊ธฐ๋ฅ์ ๋ ๋ฆฝ์ํจ๋ค : ๋ฆฌํฉํ ๋ง - ๋ฉ์๋ ์ถ์ถ ํ์ฉ
-> ์๋น์ค๋ ๋น์ฆ๋์ค ๋ก์ง์ ๋ช ํํ๊ฒ ํ๊ธฐํ๋ ๋ฐฉ์์ผ๋ก ์ฌ์ฉํด์ผ ํ๋ค ~ ๊ทธ๋์ผ ์ ์ง๋ณด์ ๊ฐ ๋งคํ์ด ํจ์ฌ ์ฝ๋ค
(๋ ํฌ์งํ ๋ฆฌ๋ ๋น์ฆ๋์ค ๋ก์ง๋ณด๋ค๋ SQL ์ฟผ๋ฆฌ ๋ฑ, ๊ฐ๋ฐ ์ฉ์ด์ ๊ฐ๊น๊ฒ ์์ฑํ๋ ๊ฒ์ด ์ข๋ค)
package hello.helloSpring.service;
import hello.helloSpring.domain.Member;
import hello.helloSpring.repository.MemoryMemberRepository;
import java.util.List;
import java.util.Optional;
public class MemberService {
private final MemoryMemberRepository repository = new MemoryMemberRepository();
public Long join(Member member) {
vaildateDuplicateMember(member);
repository.save(member);
return member.getId();
}
private void vaildateDuplicateMember(Member member) {
// ์ค๋ณต ํ์ ๊ธ์ง
repository.findByname(member.getName())
.ifPresent(m ->{ // ์ต์
๋๋ก ํ๋ฒ ๊ฐ์๊ธฐ ๋๋ฌธ์ ๋ค์ํ ๋ฉ์๋ ์ฌ์ฉ๊ฐ๋ฅ
throw new IllegalArgumentException("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค");
});
// get().orElseGet()๋ ๋ง์ด ์ฌ์ฉํ๋ค
// ๊ธฐ๋ฅ์ ๊ฐ๋ฅํ ์ถ์์ํจ๋ค : ๋ฉ์๋๋ก ๋
๋ฆฝ์ํจ๋ค
}
public List<Member> findMembers() {
return repository.findAll();
}
public Optional<Member> findOne(Long memberId) {
return repository.findById(memberId);
}
}
ํ์ ์๋น์ค ํ ์คํธ
*ํ๋๋ก ์ฌ์ฉํ๊ธฐ ์ํด ๋์ผํ ํด๋์ค ํ์ ์ ๊ฐ์ฒด๋ฅผ ๊ฐ ํ ์คํธ ๋ฑ์์ ๋งค๋ฒ ์์ฑํ๋ ๊ฒ์ ์๋ก ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ!
~ ๊ฐ์ด ๋ฌ๋ผ์ง ์๋ ์๋ค
-> ๊ทธ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ ์ธํ๊ณ ์ด๊ธฐํํ์ฌ ์ฐ๋ ๊ฒ์ด DI, IoC.
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemoryMemberRepository;
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
public void ํ์๊ฐ์
() throws Exception {
//Given
Member member = new Member();
member.setName("hello");
//When
Long saveId = memberService.join(member);
//Then
Member findMember = memberRepository.findById(saveId).get();
assertEquals(member.getName(), findMember.getName());
}
@Test
public void ์ค๋ณต_ํ์_์์ธ() throws Exception {
//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("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
}
}