솜사탕코튼 2023. 12. 25. 19:03

@ParameterizedTest

  @DisplayName("상품 타입이 재고 관련 타입인지를 체크한다.")
  @Test
  void containsStockType3() {
    // given
    ProductType givenType1 = HANDMADE;
    ProductType givenType2 = BOTTLE;
    ProductType givenType3 = BAKERY;

    // when
    boolean result1 = ProductType.containsStockType(givenType1);
    boolean result2 = ProductType.containsStockType(givenType2);
    boolean result3 = ProductType.containsStockType(givenType3);

    // then
    assertThat(result1).isFalse();
    assertThat(result2).isTrue();
    assertThat(result3).isTrue();
  }
  • 3가지 given(상황)을 줘서 결과 값 전부를 테스트해보고 싶을 때
  @DisplayName("상품 타입이 재고 관련 타입인지를 체크한다.")
  @CsvSource({"HANDMADE,false", "BOTTLE,true", "BAKERY,true"})
  @ParameterizedTest
  void containsStockType4(ProductType productType, boolean expected) {
    // when
    boolean result = ProductType.containsStockType(productType);

    // then
    assertThat(result).isEqualTo(expected);
  }
  private static Stream<Arguments> providerProductTypesForCheckingStockType() {
    return Stream.of(
        Arguments.of(HANDMADE, false),
        Arguments.of(BOTTLE, true),
        Arguments.of(BAKERY, true)
    );
  }

  @DisplayName("상품 타입이 재고 관련 타입인지를 체크한다.")
  @MethodSource("providerProductTypesForCheckingStockType")
  @ParameterizedTest
  void containsStockType5(ProductType productType, boolean expected) {
    // when
    boolean result = ProductType.containsStockType(productType);

    // then
    assertThat(result).isEqualTo(expected);
  }

https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

 

JUnit 5 User Guide

Although the JUnit Jupiter programming model and extension model do not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custo

junit.org

https://spockframework.org/spock/docs/2.3/data_driven_testing.html#data-tables

 

Data Driven Testing

Like with data pipes, you can also assign to multiple variables in one expression, if you have some object Groovy can iterate over. Unlike with data pipes, the syntax here is identical to standard Groovy multi-assignment syntax: @Shared sql = Sql.newInstan

spockframework.org