Skip to content

Commit 2781e27

Browse files
authored
Merge pull request eugenp#3558 from nguyennamthai/BAEL-1515
Initial commit for BAEL-1515
2 parents 2169be4 + fa906a2 commit 2781e27

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎testing-modules/testing/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@
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

@@ -164,6 +174,7 @@
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>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)