[x-asserts-in-one] Single Assert Call for Multiple Properties in JUnit5 (#13413)

This commit is contained in:
Kai Yuan 2023-02-04 17:19:19 +01:00 committed by GitHub
parent c0be80a64a
commit 0e1a3beb99
2 changed files with 103 additions and 2 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>junit-5-advanced</artifactId>
<version>1.0-SNAPSHOT</version>
@ -27,6 +27,12 @@
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -46,6 +52,7 @@
<properties>
<jmockit.version>1.49</jmockit.version>
<assertj.version>3.24.2</assertj.version>
</properties>
</project>

View File

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