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

02/22(๋ชฉ) ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ Java 42์ผ์ฐจ TIL - ํ…Œ์ŠคํŠธ์ฝ”๋“œ(2)

by ๊ฐ์ž๊ฐœ๋ฐœ๊พผ 2024. 2. 22.

 

๐Ÿ“–  ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

  • ํ…Œ์ŠคํŠธ์ฝ”๋“œ

 

๐Ÿฅ”  ํ…Œ์ŠคํŠธ ๋ฐ˜๋ณตํ•˜๊ธฐ

@RepeatedTest

: @RepeatedTest๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

: name ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค์ด๋ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

: RepeatitionInfo ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ํ˜„์žฌ ๋ฐ˜๋ณต ํšŸ์ˆ˜์™€ ์ด ํšŸ์ˆ˜ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@RepeatedTest(value = 5, name = "๋ฐ˜๋ณต ํ…Œ์ŠคํŠธ {currentRepetition} / {totalRepetitions}")
void repeatTest(RepetitionInfo info) {
    System.out.println("ํ…Œ์ŠคํŠธ ๋ฐ˜๋ณต : " + info.getCurrentRepetition() + " / " + info.getTotalRepetitions());
}

 

 

@ParameterizedTest

: @ParameterizedTest๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

: @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9})๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  - ์ „๋‹ฌ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜ ๋งŽํผ ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.

  - int, String ๋“ฑ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@DisplayName("ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ํ™œ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ")
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9})
void parameterTest(int num) {
    System.out.println("5 * num = " + 5 * num);
}

 

 

๐Ÿฅ”  Assertions

- Calculator

๋”๋ณด๊ธฐ
public class Calculator {
    public Double operate(double num1, String op, double num2) {
        switch (op) {
            case "*":
                return num1 * num2;
            case "/":
                if (validateNum(num2)) {
                    return num1 / num2;
                } else {
                    return null;
                }
            case "+":
                return num1 + num2;
            case "-":
                return num1 - num2;
            default:
                throw new IllegalArgumentException("์ž˜๋ชป๋œ ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.");
        }
    }

    public boolean validateNum(double num) {
        if (num == 0) {
            return false;
        } else {
            return true;
        }
    }
}

 

 

- Assertions.assertEquals(expected, actual)

@Test
@DisplayName("assertEquals")
void test1() {
    Double result = calculator.operate(5, "/", 2);
    assertEquals(2.5, result);
}

@Test
@DisplayName("assertEquals - Supplier")
void test1_1() {
    Double result = calculator.operate(5, "/", 0);
    // ํ…Œ์ŠคํŠธ ์‹คํŒจ ์‹œ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ (new Supplier<String>())
    assertEquals(2.5, result, () -> "์—ฐ์‚ฐ์ž ํ˜น์€ ๋ถ„๋ชจ๊ฐ€ 0์ด ์•„๋‹Œ์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”!");
}

@Test
@DisplayName("assertNotEquals")
void test1_2() {
    Double result = calculator.operate(5, "/", 0);
    assertNotEquals(2.5, result);
}

 

- assertEquals() ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์˜ˆ์ƒ๊ฐ’์„ ๋„ฃ๊ณ  ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฒฐ๊ณผ๊ฐ’์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

- ๋‘ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด ํ…Œ์ŠคํŠธ์— ์‹คํŒจํ•จ.

- 3๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์— ๋žŒ๋‹ค์‹์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋„ฃ์œผ๋ฉด ํ…Œ์ŠคํŠธ ์‹คํŒจ ์‹œ ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•จ ( new Supplier<String>() )

 

 

- Assertions.assertTrue(boolean)

@Test
@DisplayName("assertTrue ์™€ assertFalse")
void test2() {
    assertTrue(calculator.validateNum(9));
    assertFalse(calculator.validateNum(0));
}

 

- assertTrue() ๋ฉ”์„œ๋“œ๋Š” ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด true์ธ์ง€ ํ™•์ž„ํ•จ.

 

- Assertions.assertNotNull(actual)

@Test
@DisplayName("assertNotNull ๊ณผ assertNull")
void test3() {
    Double result1 = calculator.operate(5, "/", 2);
    assertNotNull(result1);
    Double result2 = calculator.operate(5, "/", 0);
    assertNull(result2);
}

 

- assertNotNull() ๋ฉ”์„œ๋“œ๋Š” ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด null์ด ์•„๋‹˜์„ ํ™•์ธํ•จ.

 

 

- Assertions.assertThrows(expeatedType, executable)

@Test
@DisplayName("assertThrows")
void test4() {
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> calculator.operate(5, "?", 2));
    assertEquals("์ž˜๋ชป๋œ ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.", exception.getMessage());
}

 

- assertThrows() ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์˜ˆ์ƒํ•˜๋Š” Exception ํด๋ž˜์Šค ํƒ€์ž…์„ ๋„ฃ๊ณ  ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์‹คํ–‰ ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š”๋‹ค.

- ์‹คํ–‰ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ƒํ•œ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ํƒ€์ž…์ด๋ผ๋ฉด ํ…Œ์ŠคํŠธ์— ์„ฑ๊ณต

 

 

๐Ÿ“š  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

์–ด๋ ต๋‹ค ํ…Œ์ŠคํŠธ์ฝ”๋“œ ํ•˜์ง€๋งŒ ํ™”์ดํŒ