Junit параметризует ввод и утверждение - PullRequest
1 голос
/ 08 января 2020

У меня есть похожие тесты, когда отличаются только значения ввода / утверждения:

@Test
void test1() {
    // Given:
    val request = Request
            .builder()
            .totalAmount(new BigDecimal("160000"))
            .termInYears(20)
            .familySize(2)
            .incomeAmount(new BigDecimal("4000"))
            .costOfLiving(BigDecimal.ZERO)
            .build();

    // When:
    Result result =
            facade.getLoanAmount(request);

    // Then:
    result.getSimulations().forEach(simulation -> {
        if(simulation.getVariantCode().equals(Variant.A)) {
            assertThat(simulation.getInstallmentAmount().compareTo(new BigDecimal("720")) == 0);
            assertThat(simulation.getInterestRate().compareTo(new BigDecimal("0.0321")) == 0);
            assertThat(simulation.getLoanAmount().compareTo(new BigDecimal("127390")) == 0);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        } if(simulation.getVariantCode().equals(Variant.B)) {
            assertThat(simulation.getInstallmentAmount().compareTo(new BigDecimal("720")) == 0);
            assertThat(simulation.getInterestRate().compareTo(new BigDecimal("0.0321")) == 0);
            assertThat(simulation.getLoanAmount().compareTo(new BigDecimal("127390")) == 0);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        } if(simulation.getVariantCode().equals(Variant.C)) {
            assertThat(simulation.getInstallmentAmount().compareTo(new BigDecimal("720")) == 0);
            assertThat(simulation.getInterestRate().compareTo(new BigDecimal("0.0321")) == 0);
            assertThat(simulation.getLoanAmount().compareTo(new BigDecimal("127390")) == 0);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        }
    });
}

@Test
void test2() {
    // Given:
    val request = Request
            .builder()
            .totalAmount(new BigDecimal("200000"))
            .termInYears(20)
            .familySize(2)
            .incomeAmount(new BigDecimal("4000"))
            .costOfLiving(BigDecimal.ZERO)
            .build();

    // When:
    Result result =
            facade.getLoanAmount(request);

    // Then:
    result.getSimulations().forEach(simulation -> {
        if(simulation.getVariantCode().equals(Variant.A)) {
            assertThat(simulation.getInstallmentAmount().compareTo(new BigDecimal("720")) == 0);
            assertThat(simulation.getInterestRate().compareTo(new BigDecimal("0.0321")) == 0);
            assertThat(simulation.getLoanAmount().compareTo(new BigDecimal("127390")) == 0);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        } if(simulation.getVariantCode().equals(Variant.B)) {
            assertThat(simulation.getInstallmentAmount().compareTo(new BigDecimal("720")) == 0);
            assertThat(simulation.getInterestRate().compareTo(new BigDecimal("0.0321")) == 0);
            assertThat(simulation.getLoanAmount().compareTo(new BigDecimal("127390")) == 0);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        } if(simulation.getVariantCode().equals(Variant.C)) {
            assertThat(simulation.getInstallmentAmount() == null);
            assertThat(simulation.getInterestRate() == null);
            assertThat(simulation.getLoanAmount()== null);
            assertThat(simulation.getNumberOfInstallmentsInMonths() == 240);
        }
    });
}

Этот код работает нормально, но мне интересно, как написать этот код лучше. Я знаю, что есть @ParameterizedTest, но я не уверен, что это уменьшит строку кода в этом сценарии и как обеспечить ввод / вывод там? Какой будет лучший подход? Мне нужно написать еще много подобных тестов, поэтому я думаю, что было бы неплохо обеспечить ввод / вывод для многих тестов. В Споке это был бы хороший стол, но я должен использовать другой инструмент.

1 Ответ

2 голосов
/ 08 января 2020

Если вы используете JUnit 5 (ParameterizedTests в 4 очень разные, но также сделали бы это возможным), что-то вроде этого должно работать:

@ParameterizedTest
@MethodSource("arguments")
void test(Request input, BigDecimal result) {
 // ...
}

public static Stream<Arguments> arguments() {
    r1 = Request.builder().build(); // add request configuration here
    r2 = Request.builder().build(); // ... and here
    return Stream.of(
       Arguments.of(r1, new BigDecimal("720")),
       Arguments.of(r2, null)
    );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...