νμ κ΄λ¦¬ μμ
4. νμ μλΉμ€ κ°λ°
1) νμ μλΉμ€ λ§λ€κΈ°
- service ν¨ν€μ§ μμ±
- MemberService ν΄λμ€ μμ±
- νμ 리ν¬μ§ν 리μ λλ©μΈμ νμ©νμ¬ μ€μ λΉμ¦λμ€ λ‘μ§μ ꡬνν κ²μ΄λ€.
- νμ 리ν¬μ§ν 리λ λ©μλλ₯Ό κ°λ°μ μΉνμ μΈ μ΄λ¦μΌλ‘ ꡬννμ§λ§, νμ μλΉμ€λ λΉμ¦λμ€ μΉνμ μΈ μ΄λ¦μΌλ‘ ꡬννλ€.
package hello.hello.spring.service;
import hello.hello.spring.domain.Member;
import hello.hello.spring.repository.MemberRepository;
import hello.hello.spring.repository.MemoryMemberRepository;
import java.util.List;
import java.util.Optional;
public class MemberService {
private final MemberRepository memberRepository = new MemoryMemberRepository();
// νμ κ°μ
public long join(Member member) {
validateDuplicateMember(member);
memberRepository.save(member);
return member.getId();
}
// λΉμ¦λμ€ λ‘μ§: κ°μ μ΄λ¦μΈ νμμ μλλ€.
private void validateDuplicateMember(Member member) {
// λ°ν κ°μ Optional μΈμλ‘ λ°μ μ¬μ©ν μλ μμ§λ§,
// λ°νλ°μ§ μκ³ λ°λ‘ μ¬μ©νλ λ°©λ²μ κΆμ₯νλ€.
memberRepository.findByName(member.getName())
.ifPresent(member1 -> { // nullμ΄ μλ κ°μ΄ μ‘΄μ¬νλ€λ©΄ {} λ©μλ μ€ν
throw new IllegalStateException("μ΄λ―Έ μ‘΄μ¬νλ νμμ
λλ€.");
});
// Optional κ°μ κΊΌλΌ λ get()μ μ¬μ©νλ λ°©λ²λ μμ§λ§,
// orElseGet()μ ν΅ν΄ κ°μ΄ μλ€λ©΄ κΊΌλ΄κ³ μλ€λ©΄ μμ λ©μλλ₯Ό μ€ννλ λ°©λ²μ λ§μ΄ μ΄λ€.
}
// μ 체 νμ μ‘°ν
public List<Member> findMembers() {
return memberRepository.findAll();
}
// νμ μ‘°ν
public Optional<Member> findOne(Long memberId) {
return memberRepository.findById(memberId);
}
}
2) ν μ€νΈ μΌμ΄μ€ μ½κ² λ§λλ λ²
Ctrl + Shift + T λλ₯Έ ν ν μ€νΈν Member νλͺ© μ ννκ³ Ok νλ©΄ ν¨ν€μ§, ν μ€νΈ Class λͺ¨λ μλμΌλ‘ μμ±λλ€.
3) νμ μλΉμ€ ν μ€νΈ μΌμ΄μ€ μμ±
- ν μ€νΈ μ½λ λ΄μμ νκΈλ‘ λ€μ΄λ°ν΄λ 무κ΄νλ€.
- ν μ€νΈ μ½λλ λΉλ μ ν¬ν¨λμ§ μλλ€.
- @BeforeEach: κ° ν
μ€νΈ μ€ν μ μ νΈμΆλλ€. ν
μ€νΈκ° μλ‘ μν₯μ΄ μλλ‘ νμ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νκ³ ,
μμ‘΄ κ΄κ³λ μλ‘ λ§Ίμ΄μ€λ€.
package hello.hello.spring.service;
import hello.hello.spring.domain.Member;
import hello.hello.spring.repository.MemberRepository;
import hello.hello.spring.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 java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class MemberServiceTest {
MemberService memberService;
MemoryMemberRepository memberRepository;
// MemoryMemberRepository μ store λ°μ΄ν°λ static μ μΈμ΄λΌ
// μ¬μ©ν λλ§λ€ new()λ‘ μμ±ν΄λ 무방νμ§λ§ κ·Έλλ κ°κ° λ€λ₯Έ κ°μ²΄λ‘ κ΄λ¦¬νλ κ²μ μ’μ§ μλ€.
// 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("hello");
// when
Long saveId = memberService.join(member);
// then
Member findMember = memberService.findOne(saveId).get();
Assertions.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));
Assertions.assertThat(e.getMessage()).isEqualTo("μ΄λ―Έ μ‘΄μ¬νλ νμμ
λλ€.");
/*
try {
memberService.join(member2); // μμΈ λ°μ 체ν¬
fail(); // μμΈ λ°μμ΄ μλλ©΄ μ€ν¨ μ²λ¦¬
} catch (IllegalStateException e){ // μμΈ λ°μ
Assertions.assertThat(e.getMessage()).isEqualTo("μ΄λ―Έ μ‘΄μ¬νλ νμμ
λλ€.");
}
*/
}
@Test
void findMembers() {
}
@Test
void findOne() {
}
}
public class MemberService {
private final MemberRepository memberRepository;
// DI: MemberService μ
μ₯μμ 보면 μΈλΆμμ κ°μ²΄λ₯Ό μ ν΄μ€λ€.
public MemberService(MemberRepository memberRepository){
this.memberRepository = memberRepository;
}
...
}