File tree Expand file tree Collapse file tree
main/java/com/baeldung/testing/assertj/custom
test/java/com/baeldung/testing/assertj/custom Expand file tree Collapse file tree Original file line number Diff line number Diff line change 154154 </execution >
155155 </executions >
156156 </plugin >
157+ <plugin >
158+ <groupId >org.assertj</groupId >
159+ <artifactId >assertj-assertions-generator-maven-plugin</artifactId >
160+ <version >${assertj-generator.version} </version >
161+ <configuration >
162+ <classes >
163+ <param >com.baeldung.testing.assertj.custom.Person</param >
164+ <param >com.baeldung.testing.assertj.custom.Car</param >
165+ </classes >
166+ </configuration >
167+ </plugin >
157168 </plugins >
158169 </build >
159170
164175 <guava .version>21.0</guava .version>
165176 <assertj-guava .version>3.1.0</assertj-guava .version>
166177 <assertj-core .version>3.6.1</assertj-core .version>
178+ <assertj-generator .version>2.1.0</assertj-generator .version>
167179 <truth .version>0.32</truth .version>
168180 <jUnitParams .version>1.1.0</jUnitParams .version>
169181 <jgotesting .version>0.12</jgotesting .version>
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ public class Assertions {
4+ public static PersonAssert assertThat (Person actual ) {
5+ return new PersonAssert (actual );
6+ }
7+
8+ public static CarAssert assertThat (Car actual ) {
9+ return new CarAssert (actual );
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ public class Car {
4+ private String type ;
5+ private Person owner ;
6+
7+ public Car (String type ) {
8+ this .type = type ;
9+ }
10+
11+ public String getType () {
12+ return type ;
13+ }
14+
15+ public Person getOwner () {
16+ return owner ;
17+ }
18+
19+ public void setOwner (Person owner ) {
20+ this .owner = owner ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import org .assertj .core .api .AbstractAssert ;
4+
5+ public class CarAssert extends AbstractAssert <CarAssert , Car > {
6+
7+ public CarAssert (Car actual ) {
8+ super (actual , CarAssert .class );
9+ }
10+
11+ public static CarAssert assertThat (Car actual ) {
12+ return new CarAssert (actual );
13+ }
14+
15+ public CarAssert hasType (String type ) {
16+ isNotNull ();
17+ if (!actual .getType ().equals (type )) {
18+ failWithMessage ("Expected type %s but was %s" , type , actual .getType ());
19+ }
20+ return this ;
21+ }
22+
23+ public CarAssert isUsed () {
24+ isNotNull ();
25+ if (actual .getOwner () == null ) {
26+ failWithMessage ("Expected old but was new" );
27+ }
28+ return this ;
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ public class Person {
7+ private String fullName ;
8+ private int age ;
9+ private List <String > nicknames ;
10+
11+ public Person (String fullName , int age ) {
12+ this .fullName = fullName ;
13+ this .age = age ;
14+ this .nicknames = new ArrayList <>();
15+ }
16+
17+ public void addNickname (String nickname ) {
18+ nicknames .add (nickname );
19+ }
20+
21+ public String getFullName () {
22+ return fullName ;
23+ }
24+
25+ public int getAge () {
26+ return age ;
27+ }
28+
29+ public List <String > getNicknames () {
30+ return nicknames ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import org .assertj .core .api .AbstractAssert ;
4+
5+ public class PersonAssert extends AbstractAssert <PersonAssert , Person > {
6+
7+ public PersonAssert (Person actual ) {
8+ super (actual , PersonAssert .class );
9+ }
10+
11+ public static PersonAssert assertThat (Person actual ) {
12+ return new PersonAssert (actual );
13+ }
14+
15+ public PersonAssert hasFullName (String fullName ) {
16+ isNotNull ();
17+ if (!actual .getFullName ().equals (fullName )) {
18+ failWithMessage ("Expected full name %s but was %s" , fullName , actual .getFullName ());
19+ }
20+ return this ;
21+ }
22+
23+ public PersonAssert isAdult () {
24+ isNotNull ();
25+ if (actual .getAge () < 18 ) {
26+ failWithMessage ("Expected adult but was juvenile" );
27+ }
28+ return this ;
29+ }
30+
31+ public PersonAssert hasNickname (String nickName ) {
32+ isNotNull ();
33+ if (!actual .getNicknames ().contains (nickName )) {
34+ failWithMessage ("Expected nickname %s but did not have" , nickName );
35+ }
36+ return this ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import static com .baeldung .testing .assertj .custom .CarAssert .assertThat ;
4+
5+ import org .junit .Rule ;
6+ import org .junit .Test ;
7+ import org .junit .rules .ExpectedException ;
8+
9+ public class AssertJCarAssertUnitTest {
10+ @ Rule
11+ public ExpectedException thrown = ExpectedException .none ();
12+
13+ @ Test
14+ public void whenCarTypeDoesNotMatch_thenIncorrect () {
15+ thrown .expect (AssertionError .class );
16+ thrown .expectMessage ("Expected type SUV but was Sedan" );
17+ Car car = new Car ("Sedan" );
18+ assertThat (car ).hasType ("SUV" );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import static com .baeldung .testing .assertj .custom .Assertions .assertThat ;
4+
5+ import org .junit .Rule ;
6+ import org .junit .Test ;
7+ import org .junit .rules .ExpectedException ;
8+
9+ public class AssertJCustomAssertionsUnitTest {
10+ @ Rule
11+ public ExpectedException thrown = ExpectedException .none ();
12+
13+ @ Test
14+ public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect () {
15+ thrown .expect (AssertionError .class );
16+ thrown .expectMessage ("Expected nickname John but did not have" );
17+ Person person = new Person ("John Doe" , 20 );
18+ person .addNickname ("Nick" );
19+ assertThat (person ).hasNickname ("John" );
20+ }
21+
22+ @ Test
23+ public void whenCarIsUsed_thenCorrect () {
24+ Person person = new Person ("Jane Roe" , 16 );
25+ Car car = new Car ("SUV" );
26+ car .setOwner (person );
27+ assertThat (car ).isUsed ();
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .testing .assertj .custom ;
2+
3+ import static com .baeldung .testing .assertj .custom .PersonAssert .assertThat ;
4+
5+ import org .junit .Rule ;
6+ import org .junit .Test ;
7+ import org .junit .rules .ExpectedException ;
8+
9+ public class AssertJPersonAssertUnitTest {
10+ @ Rule
11+ public ExpectedException thrown = ExpectedException .none ();
12+
13+ @ Test
14+ public void whenPersonNameMatches_thenCorrect () {
15+ Person person = new Person ("John Doe" , 20 );
16+ assertThat (person ).hasFullName ("John Doe" );
17+ }
18+
19+ @ Test
20+ public void whenPersonAgeLessThanEighteen_thenNotAdult () {
21+ thrown .expect (AssertionError .class );
22+ thrown .expectMessage ("Expected adult but was juvenile" );
23+ Person person = new Person ("Jane Roe" , 16 );
24+ assertThat (person ).isAdult ();
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments