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+ </classes >
165+ </configuration >
166+ </plugin >
157167 </plugins >
158168 </build >
159169
164174 <guava .version>21.0</guava .version>
165175 <assertj-guava .version>3.1.0</assertj-guava .version>
166176 <assertj-core .version>3.6.1</assertj-core .version>
177+ <assertj-generator .version>2.1.0</assertj-generator .version>
167178 <truth .version>0.32</truth .version>
168179 <jUnitParams .version>1.1.0</jUnitParams .version>
169180 <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+ 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 static com .baeldung .testing .assertj .custom .Assertions .assertThat ;
4+ import static org .junit .Assert .fail ;
5+
6+ import org .junit .Rule ;
7+ import org .junit .Test ;
8+ import org .junit .rules .ExpectedException ;
9+
10+ public class AssertJCustomAssertionsUnitTest {
11+ @ Rule
12+ public ExpectedException thrown = ExpectedException .none ();
13+
14+ @ Test
15+ public void whenPersonNameMatches_thenCorrect () {
16+ Person person = new Person ("John Doe" , 20 );
17+ assertThat (person ).hasFullName ("John Doe" );
18+ }
19+
20+ @ Test
21+ public void whenPersonAgeLessThanEighteen_thenNotAdult () {
22+ Person person = new Person ("Jane Roe" , 16 );
23+
24+ try {
25+ assertThat (person ).isAdult ();
26+ fail ();
27+ } catch (AssertionError e ) {
28+ org .assertj .core .api .Assertions .assertThat (e ).hasMessage ("Expected person to be adult" );
29+ }
30+ }
31+
32+ @ Test
33+ public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect () {
34+ Person person = new Person ("John Doe" , 20 );
35+ person .addNickname ("Nick" );
36+
37+ try {
38+ assertThat (person ).hasNickname ("John" );
39+ fail ();
40+ } catch (AssertionError e ) {
41+ org .assertj .core .api .Assertions .assertThat (e ).hasMessage ("Expected person to have nickname John" );
42+ }
43+ }
44+ }
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+ // static factory methods of other assertion classes
9+ }
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 person to have 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 person to be adult" );
27+ }
28+ return this ;
29+ }
30+
31+ public PersonAssert hasNickname (String nickName ) {
32+ isNotNull ();
33+ if (!actual .getNicknames ().contains (nickName )) {
34+ failWithMessage ("Expected person to have nickname %s" , nickName );
35+ }
36+ return this ;
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments