Merge pull request #3558 from nguyennamthai/BAEL-1515
Initial commit for BAEL-1515
This commit is contained in:
commit
2781e27d1d
|
@ -154,6 +154,16 @@
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
|
||||||
|
<version>${assertj-generator.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<classes>
|
||||||
|
<param>com.baeldung.testing.assertj.custom.Person</param>
|
||||||
|
</classes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -164,6 +174,7 @@
|
||||||
<guava.version>21.0</guava.version>
|
<guava.version>21.0</guava.version>
|
||||||
<assertj-guava.version>3.1.0</assertj-guava.version>
|
<assertj-guava.version>3.1.0</assertj-guava.version>
|
||||||
<assertj-core.version>3.6.1</assertj-core.version>
|
<assertj-core.version>3.6.1</assertj-core.version>
|
||||||
|
<assertj-generator.version>2.1.0</assertj-generator.version>
|
||||||
<truth.version>0.32</truth.version>
|
<truth.version>0.32</truth.version>
|
||||||
<jUnitParams.version>1.1.0</jUnitParams.version>
|
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||||
<jgotesting.version>0.12</jgotesting.version>
|
<jgotesting.version>0.12</jgotesting.version>
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.testing.assertj.custom;
|
||||||
|
|
||||||
|
import static com.baeldung.testing.assertj.custom.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
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 whenPersonNameMatches_thenCorrect() {
|
||||||
|
Person person = new Person("John Doe", 20);
|
||||||
|
assertThat(person).hasFullName("John Doe");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPersonAgeLessThanEighteen_thenNotAdult() {
|
||||||
|
Person person = new Person("Jane Roe", 16);
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertThat(person).isAdult();
|
||||||
|
fail();
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to be adult");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() {
|
||||||
|
Person person = new Person("John Doe", 20);
|
||||||
|
person.addNickname("Nick");
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertThat(person).hasNickname("John");
|
||||||
|
fail();
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to have nickname John");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.testing.assertj.custom;
|
||||||
|
|
||||||
|
public class Assertions {
|
||||||
|
public static PersonAssert assertThat(Person actual) {
|
||||||
|
return new PersonAssert(actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static factory methods of other assertion classes
|
||||||
|
}
|
|
@ -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 person to have full name %s but was %s", fullName, actual.getFullName());
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PersonAssert isAdult() {
|
||||||
|
isNotNull();
|
||||||
|
if (actual.getAge() < 18) {
|
||||||
|
failWithMessage("Expected person to be adult");
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PersonAssert hasNickname(String nickName) {
|
||||||
|
isNotNull();
|
||||||
|
if (!actual.getNicknames().contains(nickName)) {
|
||||||
|
failWithMessage("Expected person to have nickname %s", nickName);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue