(3) 회원 관리(기본 툴 + 테스트)

로쿠's avatar
Sep 04, 2024
(3) 회원 관리(기본 툴 + 테스트)
Contents
1) 설계

1) 설계

  • 데이터 : 회원 ID , 이름
  • 기능 : 회원 등록 및 조회
  • DB는 아직 미정
  • 계층구조
    • 컨트롤러 ⇒ 서비스 ⇒ 리포지토리 ⇒ DB
    • (컨트롤러 ⇒ 도메인, 서비스 ⇒ 도메인, 리포지토리 ⇒ 도메인)
    • 컨트롤러 : 웹MVC의 컨트롤러
    • 서비스 : 핵심 비즈니스 로직 구현(회원 중복 가입 금지 등)
    • 리포지토리 : DB에 접근, 도메인 객체를 DB에 저장 및 관리
    • 도메인 : 비즈니스 도메인 객체 (회원, 주문, 쿠폰 등)
  • 클래스 의존 관계
    • DB가 미정인 상황, 인터페이스로 구현하여 클래스를 변경할 수 있도록 설계
    • 개발을 진행하기 위해 구현체로 가벼운 메모리 기반의 DB사용
notion image
com.example.luca.helloSpring > domain > Member (id, name)
package com.example.luca.helloSpring.domain;
public class Member {
private Long id; // 시스템상 저장하기 위한 ID
private String name;
 
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
com.example.luca.helloSpring > repository > MemberRepository
package com.example.luca.helloSpring.repository;
import com.example.luca.helloSpring.domain.Member;
import java.util.List;
import java.util.Optional;
 
public interface MemberRepository {
Member save(Member member); // null 처리를 위해 optional 사용
Optional<Member> findById(Long id);
Optional<Member> findByName(String name);
List<Member> findAll();
}
com.example.luca.helloSpring > repository > MemoryMemberRepository
package com.example.luca.helloSpring.repository;
import com.example.luca.helloSpring.domain.Member;
import java.util.HashMap;
import java.util.List;import java.util.Map;
import java.util.Optional;
 
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());
}
}

2) 회원 리포지토리 테스트 케이스

  • 생성하기
notion image
테스트를 할 때는 보통 패키지를 똑같이 만들어 준다.
그 후 테스트 하려는 클래스 네임 + Test 로 클래스를 만들어 준다.
 
  • 작성 방법
notion image
class는 공유하지 않기 때문에 public 을 안 붙여줘도 된다.
 
  • 작동 방법
notion image
public void save() 를 만들어 주면 왼쪽에 초록 표시(Run)가 뜬다.
 
notion image
코드를 다 입력한 후 작동하면 체크 표시가 뜬다.
에러가 있으면 엑스 표시가 뜬다.
notion image
Ruka1과 Ruka2를 만들어 준 후
member1 에 result인 Ruka1이 있는 지 확인한 후 정상적으로 작동되면 체크 표시가 뜬다.
notion image
Ruka2 를 넣으면
notion image
이렇게 된다.
 
  • 전체 작동
 
notion image
메소드 하나씩 테스트를 할 수도 있지만 클래스 전체 테스트를 돌릴 수 있다.
notion image
 
  • 데이터클리어(중요)
notion image
notion image
findAll 을 추가 해준 뒤 개별적으로 작동확인 후 클래스 전체를 작동시키면 에러가 뜬다.
순서는 보장되지 않기 때문에 이미 같은 이름이 생성되어 있음 그렇기 때문에 데이터 클리어를 해줘야 한다.
notion image
com.example.luca.helloSpring > repository > MemoryMemberRepository 에
public void clearStore() { store.clear();} 추가
notion image
Test에
@AfterEach //
public void afterEach() { repository.clearStore();} 추가
notion image
 
그 후 클래스를 재실행해보면 문제없이 작동된다.
Share article