gpt4 book ai didi

java - Junit 参数化输入和断言

转载 作者:行者123 更新时间:2023-11-30 01:41:53 25 4
gpt4 key购买 nike

当只有输入/断言值不同时,我有类似的测试:

@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 但我不确定这会减少这种情况下的代码行以及如何在那里提供输入/输出?最好的方法是什么?我必须编写更多这样的测试,所以我认为为许多测试提供输入/输出会很好。在 Spock 中,这将是一个不错的表,但我必须使用另一个工具。

最佳答案

如果您使用 JUnit 5(4 中的 ParameterizedTests 非常不同,但也使其成为可能),类似这样的事情应该可以工作:

@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)
);
}

关于java - Junit 参数化输入和断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com