본 프로젝트 자료는 김영한님 스프링 핵심 원리 - 기본편을 참고로 제작됐습니다.
사전 준비물
스프링을 제외한 자바 코드로만 사용한다해서 Dependencies를 추가하지 않았다
요구사항은 확정이 아니라 진행할 수 없는 상태지만, 지연할 수 없어 앞에 배운 객체 지향 설계 방법을 사용하자. 중간에 바꾸기를 원하면 갈아끼울 수 있도록 설계하면 된다.
package hello.core.member;
public enum Grade {
BASIC,
VIP
}
package hello.core.member;
public class Member {
private Long id;
private String name;
private Grade grade;
public Member(Long id, String name, Grade grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
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;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
package hello.core.member;
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
package hello.core.member;
import java.util.HashMap;
import java.util.Map;
public class MemoryMemberRepository implements MemberRepository {
// 저장소
private static Map<Long, Member> store = new HashMap<>();
// 저장할 소스
@Override
public void save(Member member) {
store.put(member.getId(), member);
}
// 아이디 찾기
@Override
public Member findById(Long memberId) {
return store.get(memberId);
}
}
package hello.core.member;
public interface MemberService {
//회원가입
void join(Member member);
//회원조회
Member findMember(Long memberId);
}
package hello.core.member;
public class MemberServiceImpl implements MemberService {
// 저장소
private final MemberRepository memberRepository = new MemoryMemberRepository();
// 회원가입
@Override
public void join(Member member) {
memberRepository.save(member);
}
// 회원조회
@Override
public Member findMember(Long memberId) {
return memberRepository.findById(memberId);
}
}
package hello.core;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
public class MemberApp {
public static void main(String[] args) {
MemberService memberService = new MemberServiceImpl();
Member member = new Member(1L, "memberA", Grade.VIP);
memberService.join(member);
Member findMember = memberService.findMember(1L);
System.out.println("new member = " + member.getName());
System.out.println("find Member = " + findMember.getName());
}
}
애플리케이션 로직으로 이렇게 테스트하는건 좋은 방법이 아니다. JUnit 테스트를 사용하자
package hello.core.member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class MemberServiceTest {
//Member domain design
MemberService memberService = new MemberServiceImpl();
@Test
void join() {
// given
Member member = new Member(1L, "memberA", Grade.VIP);
// when
memberService.join(member);
Member findMember = memberService.findMember(1L);
// then
assertThat(member).isEqualTo(findMember);
}
}
다행이도 문제 없이 정상적으로 테스트 수행이 끝났다.
위 문제점들은 다음 글 작성할 때 수정해 나갈 것이다.