From 0e1a3beb994866c66c0c03b471dfabf676f11aad Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sat, 4 Feb 2023 17:19:19 +0100 Subject: [PATCH] [x-asserts-in-one] Single Assert Call for Multiple Properties in JUnit5 (#13413) --- testing-modules/junit-5-advanced/pom.xml | 11 ++- .../MultiAssertionsInOneUnitTest.java | 94 +++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 testing-modules/junit-5-advanced/src/test/java/com/baeldung/multiassertions/MultiAssertionsInOneUnitTest.java diff --git a/testing-modules/junit-5-advanced/pom.xml b/testing-modules/junit-5-advanced/pom.xml index f3e8d3f9a3..bfc490b03e 100644 --- a/testing-modules/junit-5-advanced/pom.xml +++ b/testing-modules/junit-5-advanced/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 junit-5-advanced 1.0-SNAPSHOT @@ -27,6 +27,12 @@ ${jmockit.version} test + + org.assertj + assertj-core + ${assertj.version} + test + @@ -46,6 +52,7 @@ 1.49 + 3.24.2 \ No newline at end of file diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/multiassertions/MultiAssertionsInOneUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/multiassertions/MultiAssertionsInOneUnitTest.java new file mode 100644 index 0000000000..6fec9015cf --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/multiassertions/MultiAssertionsInOneUnitTest.java @@ -0,0 +1,94 @@ +package com.baeldung.multiassertions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.from; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigDecimal; +import org.junit.jupiter.api.Test; + +public class MultiAssertionsInOneUnitTest { + private static final Product EXPECTED = new Product(42L, "LG Monitor", "32 inches, 4K Resolution, Ideal for programmer", true, new BigDecimal("429.99"), 77); + private static final Product TO_BE_TESTED = new Product(-1L, "LG Monitor", "dummy value: whatever", true, new BigDecimal("429.99"), 77); + + @Test + void givenAProductObject_whenUsingAssertAll_thenAssertMultiPropertiesInOneCall() { + //@formatter:off + assertAll("Verify Product properties", + () -> assertEquals(EXPECTED.getName(), TO_BE_TESTED.getName()), + () -> assertEquals(EXPECTED.isOnSale(), TO_BE_TESTED.isOnSale()), + () -> assertEquals(EXPECTED.getStockQuantity(), TO_BE_TESTED.getStockQuantity()), + () -> assertEquals(EXPECTED.getPrice(), TO_BE_TESTED.getPrice())); + //@formatter:on + } + + @Test + void givenAProductObject_whenUsingAssertJExtracting_thenAssertMultiPropertiesInOneCall() { + //@formatter:off + assertThat(TO_BE_TESTED) + .extracting("name", "onSale", "stockQuantity", "price") + .containsExactly(EXPECTED.getName(), EXPECTED.isOnSale(), EXPECTED.getStockQuantity(), EXPECTED.getPrice()); + + assertThat(TO_BE_TESTED) + .extracting(Product::getName, Product::isOnSale, Product::getStockQuantity,Product::getPrice) + .containsExactly(EXPECTED.getName(), EXPECTED.isOnSale(), EXPECTED.getStockQuantity(), EXPECTED.getPrice()); + //@formatter:on + } + + @Test + void givenAProductObject_whenUsingAssertJReturns_thenAssertMultiPropertiesInOneCall() { + //@formatter:off + assertThat(TO_BE_TESTED) + .returns(EXPECTED.getName(),from(Product::getName)) + .returns(EXPECTED.isOnSale(), from(Product::isOnSale)) + .returns(EXPECTED.getStockQuantity(), from(Product::getStockQuantity)) + .returns(EXPECTED.getPrice(), from(Product::getPrice)) + + .doesNotReturn(EXPECTED.getId(), from(Product::getId)) + .doesNotReturn(EXPECTED.getDescription(), from(Product::getDescription)); + //@formatter:on + } +} + +class Product { + private Long id; + private String name; + private String description; + private boolean onSale; + private BigDecimal price; + private int stockQuantity; + + public Product(Long id, String name, String description, boolean onSale, BigDecimal price, int stockQuantity) { + this.id = id; + this.name = name; + this.description = description; + this.onSale = onSale; + this.price = price; + this.stockQuantity = stockQuantity; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public boolean isOnSale() { + return onSale; + } + + public BigDecimal getPrice() { + return price; + } + + public int getStockQuantity() { + return stockQuantity; + } +} \ No newline at end of file