[BAEL-5976]: Extract Values using AssertJ in Java (#13105)

JIRA: https://jira.baeldung.com/browse/BAEL-5976
This commit is contained in:
Carlos Chacin
2022-12-13 16:55:12 -08:00
committed by GitHub
parent abd9f8a38b
commit 78124d60dd
4 changed files with 109 additions and 0 deletions
@@ -0,0 +1,46 @@
package com.baeldung.assertj.extracting;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
class AssertJExtractingUnitTest {
static final List<Address> RESTRICTED_ADDRESSES = new ArrayList<>();
@Test
void whenUsingRegularAssertionFlow_thenCorrect() {
// Given
Person person = new Person("aName", "aLastName", new Address("aStreet", "aCity", new ZipCode(90210)));
// Then
Address address = person.getAddress();
assertThat(address).isNotNull()
.isNotIn(RESTRICTED_ADDRESSES);
ZipCode zipCode = address.getZipCode();
assertThat(zipCode).isNotNull();
assertThat(zipCode.getZipcode()).isBetween(1000L, 100_000L);
}
@Test
void whenUsingExtractingAssertionFlow_thenCorrect() {
// Given
Person person = new Person("aName", "aLastName", new Address("aStreet", "aCity", new ZipCode(90210)));
// Then
assertThat(person)
.extracting(Person::getAddress)
.isNotNull()
.isNotIn(RESTRICTED_ADDRESSES)
.extracting(Address::getZipCode)
.isNotNull()
.extracting(ZipCode::getZipcode, as(InstanceOfAssertFactories.LONG))
.isBetween(1_000L, 100_000L);
}
}