Initial commit for BAEL-1515
This commit is contained in:
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.testing.assertj.custom;
|
||||
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
|
||||
public class CarAssert extends AbstractAssert<CarAssert, Car> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String> getNicknames() {
|
||||
return nicknames;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.testing.assertj.custom;
|
||||
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
|
||||
public class PersonAssert extends AbstractAssert<PersonAssert, Person> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user