Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java] JUnit, AssertJ์˜ ๊ฐœ๋… ๋ฐ ๊ธฐ์ดˆ์ ์ธ ์‚ฌ์šฉ๋ฒ• (๋‹จ์ •๋ฌธ, ์–ด๋…ธํ…Œ์ด์…˜) ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[Java] JUnit, AssertJ์˜ ๊ฐœ๋… ๋ฐ ๊ธฐ์ดˆ์ ์ธ ์‚ฌ์šฉ๋ฒ• (๋‹จ์ •๋ฌธ, ์–ด๋…ธํ…Œ์ด์…˜)

๋น„๋น„ bibi 2021. 2. 16. 18:09

JUnit์˜ ๊ฐœ๋… ๋ฐ assert ๋ฉ”์†Œ๋“œ(๋‹จ์ •๋ฌธ), ์–ด๋…ธํ…Œ์ด์…˜

์ถœ์ฒ˜ : ๋งํฌ1, ๋งํฌ2, ๋งํฌ3, ๋งํฌ4. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ™‡โ€โ™€๏ธ๐Ÿ™‡โ€โ™€๏ธ

JUnit์ด๋ž€?

์ž๋ฐ”๋ฅผ ์œ„ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(์˜คํ”ˆ์†Œ์Šค)์ด๋‹ค.

์ž๋ฐ” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด ์ฃผ๋Š” ํ…Œ์ŠคํŠธ์šฉ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

org.junit

  • ๋‹จ์ •๋ฌธ (assert...())์œผ๋กœ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ˆ˜ํ–‰๊ฒฐ๊ณผ๋ฅผ ํŒ๋ณ„ํ•ด ์•Œ๋ ค์ค€๋‹ค.
  • ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๋„๋ก ์ง€์›ํ•œ๋‹ค.
  • ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ Testํด๋ž˜์Šค๋กœ ๋‚จ๊น€์œผ๋กœ์จ ํ…Œ์ŠคํŠธ history๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์œ„ ํ…Œ์ŠคํŠธ Unit Test๋ž€?

๋ชจ๋“  ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค test cate๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ ˆ์ฐจ.

์†Œ์Šค์ฝ”๋“œ์˜ ํŠน์ • ๋ชจ๋“ˆ์ด ์˜๋„๋Œ€๋กœ ์ •ํ™•ํžˆ ๋™์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.

System.out.println()๋กœ ๋งค๋ฒˆ ์‚ฌ๋žŒ์ด ๋™์ž‘ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๋Š” ํ…Œ์ŠคํŠธ ๋ฐ ๋””๋ฒ„๊น… ๊ณผ์ •์„ ์‰ฝ๊ฒŒ ํ•œ๋‹ค.

JUnit์˜ assert ๋ฉ”์†Œ๋“œ๋“ค

  • assertEquals(A, B) : ๊ฐ์ฒด A์™€ B๊ฐ€ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  • assertEquals(A, B, C) : ๊ฐ์ฒด A์™€ B๊ฐ€ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. C๋Š” ์˜ค์ฐจ๋ฒ”์œ„.
  • assertArrayEquals(A, B) : ๋ฐฐ์—ด A์™€ B๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  • assertSame(A, B) : ๊ฐ์ฒด A์™€ B๊ฐ€ ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
  • assertTrue(A) : ์กฐ๊ฑด A๊ฐ€ ์ฐธ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
  • assertNull(A) : ๊ฐ์ฒดA๊ฐ€ Null์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
  • assertNotNull(A) ๊ฐ์ฒด A๊ฐ€ Null์ด ์•„๋‹Œ์ง€ ํ™•์ธํ•œ๋‹ค.

assertThat()๋„ ์žˆ์ง€๋งŒ, AssertJ์˜ ์‚ฌ์šฉ๋ฒ•์ด ๋” ์‰ฌ์šด ๊ฒƒ ๊ฐ™์•„ ๋‚˜์ค‘์— AssertJ ๊ณต๋ถ€ํ•  ๋•Œ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ณ  ๊ฐ™์ด ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค.

JUnit์˜ ์–ด๋…ธํ…Œ์ด์…˜

  • @Test
    : ๋ฉ”์†Œ๋“œ ์œ„์— ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์„ ์–ธํ•ด, ํ…Œ์ŠคํŠธ ๋Œ€์ƒ ๋ฉ”์†Œ๋“œ์ž„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • @Test(timeout=๋ฐ€๋ฆฌ์ดˆ)
    : ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.
    ํ…Œ์ŠคํŠธ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ์ง€์ •๋œ ๋ฐ€๋ฆฌ์ดˆ๋ฅผ ๋„˜๊ธด๋‹ค๋ฉด ํ•ด๋‹น ํ…Œ์ŠคํŠธ๋Š” ์‹คํŒจ๋กœ ํŒ๋ณ„ํ•œ๋‹ค.
  • @Test(expected=์˜ˆ์™ธ)
    : ํ•ด๋‹น ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์„ฑ๊ณต/์‹คํŒจ๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    expected=์— ์ง€์ •๋œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋ณ„ํ•œ๋‹ค.
  • @Ignore
    : ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋œ ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋„๋ก ์ง€์ •ํ•œ๋‹ค.
  • @BeforeEach == (๊ตฌ)@Before
    : ๋ชจ๋“  @Test๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
    • ๊ฐ ํ…Œ์ŠคํŠธ ์‹œ์ž‘ ์ „์— ๊ฐ๊ฐ ํ˜ธ์ถœ๋œ๋‹ค.
    • @Test๋ฉ”์„œ๋“œ์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๋ฅผ @Before๋ฉ”์„œ๋“œ์— ์„ ์–ธํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.
    • ํ…Œ์ŠคํŠธ๋งˆ๋‹ค ๊ณตํ†ต์œผ๋กœ ์“ฐ์ด๋ฉด์„œ, ํ…Œ์ŠคํŠธ ์ „์— ๋ฆฌ์…‹๋˜์–ด์•ผ ํ•  ํ•ญ๋ชฉ์ด ๋“ค์–ด๊ฐ„๋‹ค
  • @AfterEach == (๊ตฌ)@After
    : ๋ชจ๋“  @Test๋ฉ”์†Œ๋“œ์˜ ์‹คํ–‰์ด ๋๋‚œ ๋’ค์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
    • ๊ฐ ํ…Œ์ŠคํŠธ๊ฐ€ ๋๋‚˜๊ณ  ๊ฐ๊ฐ ํ˜ธ์ถœ๋œ๋‹ค.
  • @BeforeAll == (๊ตฌ)@BeforeClass
    : ํ•ด๋‹น ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๋”ฑ ํ•œ ๋ฒˆ๋งŒ ์ˆ˜ํ–‰๋˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
  • @AfterAll == (๊ตฌ)@AfterClass
    : ํ•ด๋‹น ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๊ฐ€ ์‹คํ–‰์ด ๋๋‚œ ๋’ค์— ๋”ฑ ํ•œ ๋ฒˆ๋งŒ ์ˆ˜ํ–‰๋˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
    • ํ…Œ์ŠคํŠธํด๋ž˜์Šค์˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๊ฐ€ ์™„๋ฃŒ๋œ ๋’ค ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค.

beforeclass before test after afterclass


AssertJ

์ถœ์ฒ˜ : AssertJ , AssertJ ๊ณต์‹ ๊ฐ€์ด๋“œ, AssertJ API ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ™‡โ€โ™€๏ธ๐Ÿ™‡โ€โ™€๏ธ

AssertJ๋ž€

์ž๋ฐ” JUnit์˜ ํ…Œ์ŠคํŠธ์ฝ”๋“œ์— ์‚ฌ์šฉ๋˜์–ด, ํ…Œ์ŠคํŠธ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ํŽธ์˜์„ฑ์„ ๋†’์—ฌ ์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ.

  • ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์„ ์ง€์›ํ•ด, ๋” ์ง๊ด€์ ์ด๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ
  • ํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ ํ’๋ถ€ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ œ๊ณตํ•จ

์ž๋ฐ” 8 ์ด์ƒ์€ AssertJ 3.x๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ์ž๋ฐ” 8 ๊ธฐ์ค€, Gradle์—์„œ๋Š” testCompile 'org.assertj:assertj-core:3.6.2'๋กœ ์‚ฌ์šฉํ•œ๋‹ค

AssertJ ๋ฉ”์†Œ๋“œ ์ž„ํฌํŠธ

import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;

static ์ž„ํฌํŠธ๋ฅผ ํ†ตํ•ด AssertJ์˜ ๋‹ค์–‘ํ•œ API๋ฅผ ํด๋ž˜์Šค ์ด๋ฆ„ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ.

AssertJ์˜ assert ๋ฉ”์†Œ๋“œ

AssertJ์—์„œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋Š” assertThat()์œผ๋กœ ์‹œ์ž‘ํ•จ.

  • assertThat(ํ…Œ์ŠคํŠธ ํƒ€๊ฒŸ).๋ฉ”์†Œ๋“œ1().๋ฉ”์†Œ๋“œ2().๋ฉ”์†Œ๋“œ3()'
    ์ด๋Ÿฐ ํฌ๋งท์œผ๋กœ AssertJ์˜ ์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๋“ค์„ ์—ฐ์‡„์ ์œผ๋กœ ํ˜ธ์ถœํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
    (๋ฉ”์„œ๋“œ ์ฒด์ด๋‹)

์˜ˆ์ œ - ๋ฌธ์ž์—ด ํ…Œ์ŠคํŠธ

isNotEmpty(), contains(e), doesNotContain(e), startsWith(e), endsWith(e), isEqualTo(e)

assertThat("Hello, world! Nice to meet you.") // ์ฃผ์–ด์ง„ "Hello, world! Nice to meet you."๋ผ๋Š” ๋ฌธ์ž์—ด์€
                .isNotEmpty() // ๋น„์–ด์žˆ์ง€ ์•Š๊ณ 
                .contains("Nice") // "Nice"๋ฅผ ํฌํ•จํ•˜๊ณ 
                .contains("world") // "world"๋„ ํฌํ•จํ•˜๊ณ 
                .doesNotContain("ZZZ") // "ZZZ"๋Š” ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉฐ
                .startsWith("Hell") // "Hell"๋กœ ์‹œ์ž‘ํ•˜๊ณ 
                .endsWith("u.") // "u."๋กœ ๋๋‚˜๋ฉฐ
                .isEqualTo("Hello, world! Nice to meet you."); // "Hello, world! Nice to meet you."๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ - ์ˆซ์ž ํ…Œ์ŠคํŠธ

assertThat(3.14d) // ์ฃผ์–ด์ง„ 3.14๋ผ๋Š” ์ˆซ์ž๋Š”
                .isPositive() // ์–‘์ˆ˜์ด๊ณ 
                .isGreaterThan(3) // 3๋ณด๋‹ค ํฌ๋ฉฐ
                .isLessThan(4) // 4๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค
                .isEqualTo(3, offset(1d)) // ์˜คํ”„์…‹ 1 ๊ธฐ์ค€์œผ๋กœ 3๊ณผ ๊ฐ™๊ณ 
                .isEqualTo(3.1, offset(0.1d)) // ์˜คํ”„์…‹ 0.1 ๊ธฐ์ค€์œผ๋กœ 3.1๊ณผ ๊ฐ™์œผ๋ฉฐ
                .isEqualTo(3.14); // ์˜คํ”„์…‹ ์—†์ด๋Š” 3.14์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค

AssertJ์˜ ๋ฉ”์„œ๋“œ๋“ค

์šฐ์„ ์€ ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” org.assertj.core.api ํŒจํ‚ค์ง€์˜ ๋ฉ”์„œ๋“œ๋“ค ์ค‘์‹ฌ์œผ๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.

๋ฉ”์„œ๋“œ๋“ค์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ ๋‹น์žฅ ์“ธ ์ˆ˜ ์žˆ์„ ๋งŒํ•œ ๊ฒƒ์œผ๋กœ ์ถ”๋ ค์„œ ์ •๋ฆฌํ–ˆ๋‹ค.

(์˜์–ด๋ฅผ ์ž˜ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋˜๋Š” ๋ถ€๋ถ„๋งŒ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.)

์ถœ์ฒ˜ : assertJ ๊ณต์‹ ์›นํŽ˜์ด์ง€ - Package org.assertj.core.api

์ผ๋ฐ˜

Assert ์ธํ„ฐํŽ˜์ด์Šค
: ๋ชจ๋“  Assertion ๊ฐ์ฒด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•จ

  • isEqualTo(Object o) : ์‹ค์ œ ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ๊ฐ™์€์ง€ ํ™•์ธ
    isNotEqualTo(Object o) : ์‹ค์ œ ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ๊ฐ™์ง€ ์•Š์€์ง€ ํ™•์ธ

  • isInstanceOf(Class<?> type) , isInstanceOfAny(Class<?> ... types)
    : ์‹ค์ œ ๊ฐ’์ด ์ฃผ์–ด์ง„ ์œ ํ˜•์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธ
    isNotInstanceOf(Class<?> type), isNotInstanceOfAny(Class<?> ... types)๋Š” ๊ทธ ๋ฐ˜๋Œ€.

  • isExactlySameInstanceOf(Class<?> type)
    : ์‹ค์ œ ๊ฐ’์ด ์ •ํ™•ํžˆ ์ฃผ์–ด์ง„ ์œ ํ˜•์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธ
    isNotExactlyInstanceOf(Class<?> type) ๋Š” ๊ทธ ๋ฐ˜๋Œ€.

  • asList() : ์‹ค์ œ ๊ฐ’์ด List์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธํ•˜๊ณ  list Assertion์„ ๋ฐ˜ํ™˜

  • asString() : toString()์œผ๋กœ ์‹ค์ œ ๊ฐ’์— ๋Œ€ํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜

  • hasSameClassAs(Object o), doesNotHaveSameClassAs(Object o)
    hasSameHashCodeAs(Object o), doesNotHaveSameHashCodeAs(Object o)

    : ์‹ค์ œ ๊ฐ’ / ๊ฐ์ฒด๊ฐ€ ์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ๋™์ผํ•œ ํด๋ž˜์Šค / ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
    doesNot์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋Œ€๋กœ, ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์€์ง€๋ฅผ ํ™•์ธ

  • hasToString(String str), doesNotHaveToString(String str)
    : ์‹ค์ œ actual.toString()๊ฐ’์ด ์ฃผ์–ด์ง„ String๊ณผ ๊ฐ™์€์ง€ ํ™•์ธ
    doesNot์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋Œ€๋กœ, ๊ฐ™์ง€ ์•Š์€์ง€ ํ™•์ธ

  • isIn(Iterable<?> v), isIn(Object ... v)
    : ์ฃผ์–ด์ง„ iterable ๋˜๋Š” ๊ฐ’ ๋ฐฐ์—ด์— ์‹ค์ œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ
    isNotIn(Iterable<?> v), isNotIn(Object ... v)์€ ๊ทธ ๋ฐ˜๋Œ€.

  • isNull() : ์‹ค์ œ ๊ฐ’์ด null์ธ์ง€ ํ™•์ธ.
    isNotNull()์€ ๊ทธ ๋ฐ˜๋Œ€.

  • isSameAs(Object o) : ==๋น„๊ต๋ฅผ ์‚ฌ์šฉํ•ด ์‹ค์ œ ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ๋™์ผํ•œ์ง€ ํ™•์ธ.
    isNotSameAs(Object o)๋Š” ๊ทธ ๋ฐ˜๋Œ€.

๊ทธ ์™ธ..

  • as("์„ค๋ช…") : Assertion์„ ์„ค๋ช…ํ•˜๋Š” ๋ฉ”์„œ๋“œ. "์„ค๋ช…"์˜ ๋‚ด์šฉ์ด ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ์— ์ถœ๋ ฅ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ˆซ์ž ๊ด€๋ จ ๋ฉ”์„œ๋“œ

NumberAssert์ธํ„ฐํŽ˜์ด์Šค

  • isBetween(start, end)
    : ์‹ค์ œ ๊ฐ’์ด start์—์„œ end ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•จ. (start, end ๊ฐ’์„ ํฌํ•จํ•จ)
  • isStrictlyBetween(start, end)
    : ์‹ค์ œ ๊ฐ’์ด start์—์„œ end ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•จ. (start, end ๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š์Œ)
  • isCloseTo(expected, offset) , isCloseTo(expected, percentage)
    : ์‹ค์ œ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง„ offset / percentage ๋‚ด์—์„œ expected์— ๊ฐ€๊นŒ์šด์ง€ ํ™•์ธ.
    ์ฐจ์ด๊ฐ€ offset / percentage์™€ ๊ฐ™์œผ๋ฉด Assertion์€ ์œ ํšจํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•จ.
  • isNotCloseTo(expected, offset) , isNotCloseTo(expected, percentage)
    : ์‹ค์ œ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง„ offset / percentage ๋‚ด์—์„œ expected์— ๊ฐ€๊น์ง€ ์•Š์€์ง€ ํ™•์ธ.
    ์ฐจ์ด๊ฐ€ offset / percentage์™€ ๊ฐ™์œผ๋ฉด Assertion์€ ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•จ.
  • isPositive(), isNegative()
    : ์‹ค์ œ ๊ฐ’์ด ์–‘์ˆ˜์ธ์ง€ / ์Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•จ.
  • isNotPositive(), isNotNegative()
    : ์‹ค์ œ ๊ฐ’์ด ์–‘์ˆ˜๊ฐ€ ์•„๋‹Œ์ง€ (์Œ์ˆ˜์ด๊ฑฐ๋‚˜ 0์ธ์ง€) / ์Œ์ˆ˜๊ฐ€ ์•„๋‹Œ์ง€ (์–‘์ˆ˜์ด๊ฑฐ๋‚˜ 0์ธ์ง€) ํ™•์ธํ•จ.
  • isZero() : ์‹ค์ œ ๊ฐ’์ด 0์ธ์ง€ ํ™•์ธ.
    isNotZero() :์‹ค์ œ ๊ฐ’์ด 0์ด ์•„๋‹Œ์ง€ ํ™•์ธ.
  • isOne() : ์‹ค์ œ ๊ฐ’์ด 1๊ณผ ๊ฐ™์€์ง€ ํ™•์ธ

๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž ๊ด€๋ จ ๋ฉ”์„œ๋“œ

FloatingPointNumberAssert ์ธํ„ฐํŽ˜์ด์Šค

์ˆซ์ž ๊ด€๋ จ ๋ฉ”์„œ๋“œ์— ๋”ํ•ด, ์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • isNaN() : ์‹ค์ œ ๊ฐ’์ด NaN๊ณผ ๊ฐ™์€์ง€ ํ™•์ธ.

    isNotNaN() : ์‹ค์ œ ๊ฐ’์ด NaN๊ณผ ๊ฐ™์ง€ ์•Š์€์ง€ ํ™•์ธ.

  • isFinite(), isInfinite()

AssertJ ์‚ฌ์šฉ์‹œ ์ฃผ์˜์ 

  • assertThat(Object o)๋กœ ํ…Œ์ŠคํŠธํ•  ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•œ ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

    • BAD : assertThat(actual.equals(expected));
      GOOD : assertThat(actual).isEqualTo(expected);
    • BAD : assertThat(1 == 2);
      GOOD : assertThat(1).isEqualTo(2);
  • as()๋Š” assertion ๋ฉ”์†Œ๋“œ๋“ค์„ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

    • BAD

      // DON'T DO THIS ! as/describedAs have no effect after the assertion
      assertThat(actual).isEqualTo(expected).as("description");
      assertThat(actual).isEqualTo(expected).describedAs("description");
    • GOOD

      // DO THIS: use as/describedAs before the assertion
      assertThat(actual).as("description").isEqualTo(expected);
      assertThat(actual).describedAs("description").isEqualTo(expected);

https://steady-coding.tistory.com/349?category=861122 JUnit ์ •๋ฆฌ
https://steady-coding.tistory.com/351 AssertJ ์ •๋ฆฌ
๋‚˜์ค‘์— ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ ํ›„ ์ •๋ฆฌํ•˜๊ธฐ!