스프링/3. 스프링 MVC

17. 회원관리 웹 애플리케이션 개요

sdafdq 2023. 8. 5. 17:13

회원정보

이름

나이

 

기능

회원목록 조회

회원 등록

 

 

@Getter @Setter
public class Member {
    private Long id;
    private String name;
    private int age;

    public Member(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Member(){

    }
}

멤버

 

 

public class MemberRepository {

//    실무에선 동시성 문제 때문에 ConcurrentHashMap, AtomicLong 고려
    private static Map<Long, Member> store = new HashMap<>();
    private static long sequence = 0L;
    private static final MemberRepository instance = new MemberRepository();

    public static MemberRepository getInstance(){
        return instance;
    }
    private MemberRepository(){

    }

    public Member save(Member member){
        member.setId(sequence++);
        store.put(member.getId(), member);
        return member;
    }

    public Member findById(Long id){
        return store.get(id);
    }

    public List<Member> findAll(){
        return new ArrayList<>(store.values());
    }

    public void clearStore(){
        store.clear();
    }
}

멤버 메모리 저장소. 싱글톤

clearStore()는 테스트용

 

 

public class MemberRepositoryTest {
    MemberRepository memberRepository = MemberRepository.getInstance();

    @AfterEach
    void afterEach(){
        memberRepository.clearStore();
    }


    @Test
    void save(){
        //given
        Member member = new Member("hello",20);
        
        //when
        Member savedMember = memberRepository.save(member);
        
        //then
        Member findMember = memberRepository.findById(savedMember.getId());
        assertThat(savedMember).isSameAs(findMember);
    }

    @Test
    void findAll(){
        Member mem1 = new Member("hello1", 20);
        memberRepository.save(mem1);
        Member mem2 = new Member("hello2", 30);
        memberRepository.save(mem2);

        List<Member> result =  memberRepository.findAll();
        assertThat(result.size()).isEqualTo(2);
        assertThat(result).contains(mem1,mem2);

    }


}

테스트. 하나 다 끝난 후 clear해 줌.

contains 포함하고 있는지.

'스프링 > 3. 스프링 MVC' 카테고리의 다른 글

19. JSP 회원관리 웹 애플리케이션  (0) 2023.08.05
18. 서블릿 회원관리 애플리케이션  (0) 2023.08.05
16. HTTP 응답 HTTP API - JSON  (0) 2023.08.05
15. http 응답 방식  (0) 2023.08.05
14. HttpServletResponse  (0) 2023.08.05