diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 1fd6357b87..6f185d3b4c 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -154,6 +154,17 @@ + + org.assertj + assertj-assertions-generator-maven-plugin + ${assertj-generator.version} + + + com.baeldung.testing.assertj.custom.Person + com.baeldung.testing.assertj.custom.Car + + + @@ -164,6 +175,7 @@ 21.0 3.1.0 3.6.1 + 2.1.0 0.32 1.1.0 0.12 diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java new file mode 100644 index 0000000000..5c72eb6d05 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java @@ -0,0 +1,11 @@ +package com.baeldung.testing.assertj.custom; + +public class Assertions { + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + public static CarAssert assertThat(Car actual) { + return new CarAssert(actual); + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java new file mode 100644 index 0000000000..e52ffee8e5 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java @@ -0,0 +1,22 @@ +package com.baeldung.testing.assertj.custom; + +public class Car { + private String type; + private Person owner; + + public Car(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public Person getOwner() { + return owner; + } + + public void setOwner(Person owner) { + this.owner = owner; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java new file mode 100644 index 0000000000..413c2d3e12 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java @@ -0,0 +1,30 @@ +package com.baeldung.testing.assertj.custom; + +import org.assertj.core.api.AbstractAssert; + +public class CarAssert extends AbstractAssert { + + public CarAssert(Car actual) { + super(actual, CarAssert.class); + } + + public static CarAssert assertThat(Car actual) { + return new CarAssert(actual); + } + + public CarAssert hasType(String type) { + isNotNull(); + if (!actual.getType().equals(type)) { + failWithMessage("Expected type %s but was %s", type, actual.getType()); + } + return this; + } + + public CarAssert isUsed() { + isNotNull(); + if (actual.getOwner() == null) { + failWithMessage("Expected old but was new"); + } + return this; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java new file mode 100644 index 0000000000..34afc480e4 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.testing.assertj.custom; + +import java.util.ArrayList; +import java.util.List; + +public class Person { + private String fullName; + private int age; + private List nicknames; + + public Person(String fullName, int age) { + this.fullName = fullName; + this.age = age; + this.nicknames = new ArrayList<>(); + } + + public void addNickname(String nickname) { + nicknames.add(nickname); + } + + public String getFullName() { + return fullName; + } + + public int getAge() { + return age; + } + + public List getNicknames() { + return nicknames; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java new file mode 100644 index 0000000000..4c071660f3 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java @@ -0,0 +1,38 @@ +package com.baeldung.testing.assertj.custom; + +import org.assertj.core.api.AbstractAssert; + +public class PersonAssert extends AbstractAssert { + + public PersonAssert(Person actual) { + super(actual, PersonAssert.class); + } + + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + public PersonAssert hasFullName(String fullName) { + isNotNull(); + if (!actual.getFullName().equals(fullName)) { + failWithMessage("Expected full name %s but was %s", fullName, actual.getFullName()); + } + return this; + } + + public PersonAssert isAdult() { + isNotNull(); + if (actual.getAge() < 18) { + failWithMessage("Expected adult but was juvenile"); + } + return this; + } + + public PersonAssert hasNickname(String nickName) { + isNotNull(); + if (!actual.getNicknames().contains(nickName)) { + failWithMessage("Expected nickname %s but did not have", nickName); + } + return this; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java new file mode 100644 index 0000000000..d438cc42f6 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.CarAssert.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJCarAssertUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenCarTypeDoesNotMatch_thenIncorrect() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected type SUV but was Sedan"); + Car car = new Car("Sedan"); + assertThat(car).hasType("SUV"); + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java new file mode 100644 index 0000000000..8b800de3db --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJCustomAssertionsUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected nickname John but did not have"); + Person person = new Person("John Doe", 20); + person.addNickname("Nick"); + assertThat(person).hasNickname("John"); + } + + @Test + public void whenCarIsUsed_thenCorrect() { + Person person = new Person("Jane Roe", 16); + Car car = new Car("SUV"); + car.setOwner(person); + assertThat(car).isUsed(); + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java new file mode 100644 index 0000000000..ab421915af --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.PersonAssert.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJPersonAssertUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenPersonNameMatches_thenCorrect() { + Person person = new Person("John Doe", 20); + assertThat(person).hasFullName("John Doe"); + } + + @Test + public void whenPersonAgeLessThanEighteen_thenNotAdult() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected adult but was juvenile"); + Person person = new Person("Jane Roe", 16); + assertThat(person).isAdult(); + } +}