๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

JUnit5 assertThat vs assertAll vs assertSoftly

by ์ฃผ๋ฐœ2 2022. 9. 17.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž JUnit5 assertThat vs assertAll vs assertSoftly

ํ‰์†Œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ assertThat ํ˜น์€ assertAll ๋‚ด๋ถ€์— assertThat์„ ์ž‘์„ฑํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ด๋ฒˆ์— SoftAssertions์˜ assertSoftly๋ผ๋Š” ๊ฒ€์ฆ ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ๊ฒŒ๋˜์–ด ๊ฐ„๋žตํžˆ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ˜€

 

 

 

โœ๏ธ  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertAll;

public class ExampleTest {

    private final int num1 = 5;
    private final int num2 = 10;

    private final int actual = num1 + num2;

    @Test
    void assertThatTest() {
        assertThat(actual).isEqualTo(15);
        assertThat(actual).isEqualTo(20);
        assertThat(actual).isEqualTo(15);
        assertThat(actual).isEqualTo(10);
    }

    @Test
    void assertAllTest() {
        assertAll(
                () -> assertThat(actual).isEqualTo(15),
                () -> assertThat(actual).isEqualTo(20),
                () -> assertThat(actual).isEqualTo(15),
                () -> assertThat(actual).isEqualTo(10)
        );
    }

    @Test
    void assertSoftlyTest() {
        assertSoftly(softly -> {
            softly.assertThat(actual).isEqualTo(15);
            softly.assertThat(actual).isEqualTo(20);
            softly.assertThat(actual).isEqualTo(15);
            softly.assertThat(actual).isEqualTo(10);
        });
    }
}

 

โœ๏ธ  assertThat

  • assert๋ฌธ์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๊ฒฝ์šฐ, ๊ฒ€์ฆ์— ์‹คํŒจํ•˜๋ฉด ์ดํ›„ assert๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š์Œ

 

 

โœ๏ธ  assertAll

  • assert๋ฌธ์ด ์—ฌ๋Ÿฌ๊ฐœ ์ด๋”๋ผ๋„ ๋ชจ๋“  assert๋ฅผ ๊ฒ€์ฆ ํ›„ ์‹คํŒจํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ์˜ˆ์ƒ๊ฐ’, ์‹ค์ œ๊ฐ’์„ ์ถœ๋ ฅ

 

 

โœ๏ธ  assertSoftly

  • assertAll()๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹คํŒจํ•˜๋“  ์„ฑ๊ณตํ•˜๋“  ๋ชจ๋“  assert๋ฌธ์„ ์‹คํ–‰ํ•œ ํ›„ ์‹คํŒจํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ์˜ˆ์ƒน๊ฐ’, ์‹ค์ œ๊ฐ’์„ ์ถœ๋ ฅ
  • assertAll()๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์‹คํŒจํ•œ ์ฝ”๋“œ์˜ ๋ผ์ธ ์œ„์น˜๊นŒ์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ์— ์œ ์šฉํ•œ ๋“ฏ ํ•จ

 

 

๐Ÿ“ ๊ฒฐ๋ก (๊ฐœ์ธ์ ์ธ ...)

  • ๋‹จ์ผ Assert -> assertThat
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ assert -> assertSoftly

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€