๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Spring] JUnit5์—์„œ์˜ Exception ์ฒ˜๋ฆฌ

by dkswnkk 2022. 3. 19.

์„œ๋ก 

JUnit5์œผ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋˜ ์™€์ค‘์— JUnit4์™€ Exception์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ณผ์ •์ด ๋‹ค๋ฅธ ์ ์ด ์žˆ์–ด์„œ ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์ค‘๋ณต ํšŒ์›์ธ์ง€๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    private void validateDuplicateMember(Member member) {
        List<Member> findMembers = memberRepository.findByName(member.getName());
        if (!findMembers.isEmpty()) {
            throw new IllegalStateException("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค.");
        }
    }

์œ„ ์ฝ”๋“œ๋Š” ์ค‘๋ณต ํšŒ์›์ผ ๋•Œ์— IllegalStateException ์˜ˆ์™ธ๋ฅผ ๋˜์ ธ์ค๋‹ˆ๋‹ค.

 

1.  JUnit4์—์„œ์˜ Exception ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 

    @Test(expected = IllegalStateException.class)
    public void ์ค‘๋ณต_ํšŒ์›_์˜ˆ์™ธ() throws Exception {

        Member member1 = new Member();
        member1.setName("์•ˆ์ฃผํ˜•");

        Member member2 = new Member();
        member2.setName("์•ˆ์ฃผํ˜•");
    }โ€‹

์ œ์ผ ์ƒ๋‹จ์˜ @Test  annotation์— expected ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋Š ์˜ˆ์™ธ๊ฐ€ ํ„ฐ์ง„๋‹ค๊ณ  ์˜ˆ์ƒํ•˜๊ณ  ์„ ์–ธํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์ง€์ •๋œ ์˜ˆ์™ธ๊ฐ€ throw ๋˜์ง€ ์•Š์œผ๋ฉด ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจํ•˜๊ณ  throw ๋˜๋ฉด ํ†ต๊ณผํ•ฉ๋‹ˆ๋‹ค.

 

2.  JUnit5์—์„œ์˜ Exception ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 

 JUnit5 ํ™˜๊ฒฝ์—์„œ๋Š” ์œ„์™€๊ฐ™์ด expected ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋œจ๋ฉด์„œ ์†์„ฑ ์ž์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ์„  ์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JUnit5 ํ™˜๊ฒฝ์—์„œ๋Š” @Test annotation ์„ ํ†ตํ•ด์„œ ์˜ˆ์™ธ ๊ฐ’์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์—†๋‹ค.

JUnit5์—์„œ๋Š” assertThrows ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ƒ๋˜๋Š” ์˜ˆ์™ธ์˜ ์œ ํ˜•๊ณผ ๋žŒ๋‹ค ์‹์„ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ์ค‘์ธ ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    @Test
    public void ์ค‘๋ณต_ํšŒ์›_์˜ˆ์™ธ() throws Exception {
        //given
        Member member1 = new Member();
        member1.setName("์•ˆ์ฃผํ˜•");

        Member member2 = new Member();
        member2.setName("์•ˆ์ฃผํ˜•");
        
        memberService.join(member1);
        Assertions.assertThrows(IllegalStateException.class, () -> {
            memberService.join(member2);
        });
    }

 

๊ฒฐ๋ก 

  • JUnit4: @Test annotation expected ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ ๊ฐ’ ์ง€์ • ๊ฐ€๋Šฅ.
  • JUnit5: JUnit4 ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ, assertThrows๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ƒ๋˜๋Š” ์˜ˆ์™ธ์˜ ์œ ํ˜•๊ณผ ๋žŒ๋‹ค ์‹์„ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ์ค‘์ธ ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉ.

assertThrows() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ํŠน์ • ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ ์ฃผ์žฅ ๋…ผ๋ฆฌ๋ฅผ ๋ณด๋‹ค ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋€Œ์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ: https://www.baeldung.com/junit-assert-exception

๋Œ“๊ธ€