1. ํ์ domain ๊ณผ repository ๋ง๋ค๊ธฐ
src/main/java/com.yoonsung.firstproject/domain/Member.java
package com.yoonsung.firstproject.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;
}
}
src/main/java/com.yoonsung.firstproject/repository/MemberRepository.java
package com.yoonsung.firstproject.repository;
import com.yoonsung.firstproject.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();
}
src/main/java/com.yoonsung.firstproject/repository/MemoryMemberRepository.java
package com.yoonsung.firstproject.repository;
import com.yoonsung.firstproject.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)); // null ์ฒ๋ฆฌ
}
@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();
}
}
2. test code ์์ฑ
java ์์๋ Junit ์ผ๋ก ํ ์คํธ ์ผ์ด์ค๋ฅผ ํ๋ฐฉ์ ์์ฑ ๊ฐ๋ฅํจ.
test/java/com.yoonsung.firstproject/repository/MemoryMemberRepositoryTest.java
package com.yoonsung.firstproject.repository;
import com.yoonsung.firstproject.domain.Member;
import org.junit.jupiter.api.AfterEach;
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.clearStore();
}
@Test
public void save(){
Member member = new Member();
member.setName("spring");
repository.save(member);
Member result = repository.findById(member.getId()).get();
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 member1 = new Member();
member1.setName("spring1");
repository.save(member1);
Member member2 = new Member();
member2.setName("spring2");
repository.save(member2);
List<Member> result = repository.findAll();
assertThat(result.size()).isEqualTo(2);
}
}
ํจ์ ๋จ์์ class ๋จ์๋ก Test ๋ฅผ ์คํํ ์ ์๋ค.
3. test code ํ์ธ
Test ๊ฐ ์ ์ํ๋์๋์ง ํ์ธํ๋ ๋ฒ์ Assertion ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
import static org.assertj.core.api.Assertions.*;
//Assertions.assertEquals(member, result);
assertThat(member).isEqualTo(result);
์ฃผ์๋ถ๋ถ ๋ณด๋ค ์๋ ์ฝ๋๊ฐ ๋ ๊ฐ์์ ์ด์ด์ ์์ฃผ ์ฌ์ฉํ์ ๋ค๊ณ ํ์ จ๋น.
assertThat(member).isEqualTo(result); ์ํ ํ๋ฉด. ์ ์ ์คํ.
assertThat(member).isEqualTo(null); ์ํ ํ๋ฉด. ์๋ฌ ๋ฐ์.
์ฐธ๊ณ ๋ก, class ๋จ์๋ก Test ๋ฅผ ์คํํ์ ๋ ํจ์์ ์์๋ฅผ ๋ณด์ฅํ์ง ๋ชปํ๋ค.
ํน์ ํจ์๋ฅผ ๋จผ์ ์คํํจ์ผ๋ก์จ ์๋ฌ๊ฐ ๋ฐ์ํ ์๋ ์๋ค.
๊ทธ๋์ ํ๋์ ํ ์คํธ ํ ๊ณต์ฉ ์ ์ฅ๊ณต๊ฐ์ ํด๋ฆฌ์ดํด์ค์ผ ํ๋ค.
์๋ ๋ ํด๋์ค์ ๊ฐ๊ฐ ํจ์๋ฅผ ์ถ๊ฐํด์ฃผ์.
src/main/java/com.yoonsung.firstproject/repository/MemoryMemberRepository.java
public void clearStore(){
store.clear();
}
test/java/com.yoonsung.firstproject/repository/MemoryMemberRepositoryTest.java
@AfterEach
public void afterEach(){
repository.clearStore();
}
clear ์ฝ๋ ์ถ๊ฐ ํ ์๋ฌ ์์ด ์ ์ ์คํ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
4. ํ์ ์๋น์ค ๊ฐ๋ฐ
src/main/java/com.yoonsung.firstproject/service/MemberService.java
package com.yoonsung.firstproject.service;
import com.yoonsung.firstproject.domain.Member;
import com.yoonsung.firstproject.repository.MemberRepository;
import com.yoonsung.firstproject.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<Member> result = memberRepository.findByName(member.getName());
// result.ifPresent(m -> {
// throw new IllegalStateException("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
// });
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);
}
}
* ์ฐธ๊ณ . ์ฝ๋ ํจ์๋ก ์ถ์ถํ๊ธฐ
ํน์ ๊ธฐ๋ฅ์ ๊ฐ๋ ์ฝ๋๋ ํจ์๋ก ๋นผ์.
์ด ๋ ํธํ๊ฒ ํจ์๋ก ์ถ์ถํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ถ์ถํ๊ณ ์ถ์ ํจ์๋ฅผ ๋๋๊ทธ ํ๊ณ Ctrl + T -> Extract -> Method(Ctrl + Alt + M) ๋ฅผ ํ๋ฉด ๋๋ค.
*Ctrl + Alt + V : ํ๋ฒ์ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ.
5. ํ์ ์๋น์ค ์ฝ๋ Test
1) ๋จ์ถํค๋ก Test ๋ฅผ ์ํ ํจํค์ง, ์๋ฐํ์ผ ๋ง๋ค๊ธฐ. class ์์ Ctrl + Shift + T ๋๋ฅด๊ธฐ.
package com.yoonsung.firstproject.service;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MemberServiceTest {
@Test
void join() {
}
@Test
void findMembers() {
}
@Test
void findOne() {
}
}
2) given, when, then ์ฃผ์ ์ ์ด๋๊ณ ํจํด์ ๋ช ์ํด์ ์ฝ๋๋ฅผ ์ ์ด์ฃผ๊ธฐ. <- Tip!
package com.yoonsung.firstproject.service;
import com.yoonsung.firstproject.domain.Member;
import com.yoonsung.firstproject.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.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class MemberServiceTest {
MemberService memberService;
MemoryMemberRepository memberRepository;
// ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ์ MemoryMemberRepository ๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
// Dependency Injection
@BeforeEach
public void beforeEach(){
memberRepository = new MemoryMemberRepository();
memberService = new MemberService(memberRepository);
}
@AfterEach
public void afterEach(){
memberRepository.clearStore();
}
@Test
void join() {
//given ์ด๋ค ์ํฉ์ด ์ฃผ์ด์ก์ ๋
Member member = new Member();
member.setName("hello");
//when ์ด๊ฑธ ์คํํ์ ๋
Long saveId = memberService.join(member);
//then ์ด ๊ฒฐ๊ณผ๊ฐ ๋์์ผ ํด.
Member findMember = memberService.findOne(saveId).get();
assertThat(member.getName()).isEqualTo(findMember.getName());
}
@Test
public void ExceptionDuplicateMember(){
//given
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("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
// }
IllegalStateException e = assertThrows(IllegalStateException.class, () -> memberService.join(member2));
assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
//then
}
@Test
void findMembers() {
}
@Test
void findOne() {
}
}
* Try Catch ๋ฌธ ๋์ assertThrows ์ IllegalStateException ์ ์ด์ฉํ๋ ๋ฒ.
@Test
public void ExceptionDuplicateMember(){
//given
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("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
// }
IllegalStateException e = assertThrows(IllegalStateException.class, () -> memberService.join(member2));
assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์ฌํ๋ ํ์์
๋๋ค.");
//then
}
* Dependency Injection
MemoryMemberRepository ๋ฅผ MemberServiceTest ํด๋์ค ์์์ ๋ new ๋ก ์์ฑํ์ง ์๊ณ ์์์ ์์ฑํ MemoryMemberRepositoryTest ํด๋์ค์์ ์ฌ์ฉํ MemoryMemberRepository ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ฒ.
public class MemberService {
private final MemberRepository memberRepository;
public MemberService(MemberRepository memberRepository){
this.memberRepository = memberRepository;
}
class MemberServiceTest {
MemberService memberService;
MemoryMemberRepository memberRepository;
// ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ์ MemoryMemberRepository ๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
// Dependency Injection
@BeforeEach
public void beforeEach(){
memberRepository = new MemoryMemberRepository();
memberService = new MemberService(memberRepository);
}
'๐ Computer Science > Server' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Back-End] ์คํ๋ง ์ ๋ฌธ :: Spring ์น ๊ฐ๋ฐ ๊ธฐ์ด (0) | 2024.12.22 |
---|---|
ERD ๊ทธ๋ฆฌ๋ ์ฌ์ดํธ(DB ์ค๊ณ ๋์์ธ) (0) | 2023.11.05 |