Spring 🌱

[Spring] JUnit5와 assertJ둜 ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±ν•˜κΈ°

z.zzz 2023. 6. 19. 21:41

* JUnit5둜 ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν•˜μ˜€λ‹€.

쒋은 ν…ŒμŠ€νŠΈμ˜ 쑰건

ν…ŒμŠ€νŠΈλŠ” 각각 λ…λ¦½μ μœΌλ‘œ μ‹€ν–‰λ˜μ–΄μ•Ό ν•œλ‹€. ν…ŒμŠ€νŠΈ μˆœμ„œμ— μ˜μ‘΄κ΄€κ³„κ°€ μžˆλ‹€λ©΄ 쒋은 ν…ŒμŠ€νŠΈκ°€ μ•„λ‹ˆλ‹€!

given, when, then을 μ΄μš©ν•˜μ—¬ ν…ŒμŠ€νŠΈ μ½”λ“œμ˜ 가독성을 높인닀.


1. κ°’μ˜ λΉ„κ΅λ‘œ κΈ°λŠ₯이 μ •μƒλ™μž‘ν•˜λŠ”μ§€ κ²€μ¦ν•˜κΈ°

assertThat(λΉ„κ΅λŒ€μƒ).isEqualTo(μ‚¬μš©μžκ°€ μ˜ˆμƒν•˜λŠ” κ°’)으둜 νšŒμ›μ΄ 잘 μ €μž₯λ˜μ—ˆλŠ”μ§€ κ²€μ¦ν•œλ‹€.

 

MemoryMemberRepository.java

@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);
}

 

2. λ©”λͺ¨λ¦¬ DB λ¦¬μ…‹ν•˜κΈ°

Problem

λ‹€μŒ 두 ν…ŒμŠ€νŠΈλŠ” 각각 μ‹€ν–‰ν–ˆμ„ 땐 λ¬Έμ œκ°€ μ—†μ§€λ§Œ ν…ŒμŠ€νŠΈ 클래슀 전체λ₯Ό μ‹€ν–‰ν•˜λ©΄ μ‹€νŒ¨κ°€ λ°œμƒν•œλ‹€.

 

Why

findAll λ©”μ„œλ“œμ—μ„œ spring1, spring2μ΄λž€ 이름을 가진 νšŒμ›μ΄ 이미 μƒμ„±λ˜μ—ˆλŠ”λ° 이후 findByName λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ findAllμ—μ„œ μ €μž₯ν•œ 객체가 λ°˜ν™˜λ˜μ–΄ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

 

Solution

ν•˜λ‚˜μ˜ ν…ŒμŠ€νŠΈκ°€ λλ‚˜λ©΄ repository에 μ €μž₯된 데이터λ₯Ό μ‚­μ œν•΄μ€˜μ•Όν•œλ‹€.

@AfterEach둜 각 ν…ŒμŠ€νŠΈ λ©”μ„œλ“œ 싀행이 끝날 λ•Œλ§ˆλ‹€ repository.clearStore()ν•΄μ„œ λ©”λͺ¨λ¦¬ DBλ₯Ό λ¦¬μ…‹ν•œλ‹€.

 

MemoryMemberRepository.java

@AfterEach
public void afterEach() {
    repository.clearStore();
}

@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);
}

이전 ν…ŒμŠ€νŠΈμ—μ„œ μƒμ„±ν•œ 객체가 참쑰되며 였λ₯˜ λ°œμƒ

MemberRepository.java

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

 

3. μ˜ˆμ™Έ ν…ŒμŠ€νŠΈν•˜κΈ°

assertThrows(expectedExceptionType, executableClass)둜 μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚¨ ν›„

assertThat(e.getMessage()).isEqualTo(msg)둜 μ˜ˆμƒν•œ 였λ₯˜λ©”μ‹œμ§€μ™€ λ™μΌν•œμ§€ κ²€μ¦ν•œλ‹€.

 

MemberService.java

public Long join(Member member) {
    validateDuplicateMember(member); // 쀑볡 νšŒμ› 검증
    memberRepository.save(member);
    return member.getId();
}

private void validateDuplicateMember(Member member) {
    memberRepository.findByName(member.getName())
            .ifPresent(m -> {
                throw new IllegalStateException("이미 μ‘΄μž¬ν•˜λŠ” νšŒμ›μž…λ‹ˆλ‹€.");
            });
}

MemberServiceTest.java

@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));

    //then
    assertThat(e.getMessage()).isEqualTo("이미 μ‘΄μž¬ν•˜λŠ” νšŒμ›μž…λ‹ˆλ‹€.");

}