Issue
I have similiar tests when only input/assertion value differ:
@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);
}
});
}
This code works fine but I wonder how to write this code nicier.
I know that there is @ParameterizedTest
but i'm not sure this will reduce line of code in this scenario and how to provide input/output there? What would be best approach? I have to write many more of tests like that so I think providing input/output for many tests would be nice.
In Spock it would be a nice table but i have to use another tool.
Solution
If you're using JUnit 5 (ParameterizedTests in 4 are very different, but would also make it possible), something like this should work:
@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)
);
}
Answered By - OhleC
Answer Checked By - Pedro (JavaFixing Volunteer)