Refactor BAEL-1515

This commit is contained in:
Nam Thai Nguyen 2018-02-01 11:00:51 +07:00
parent 5f87ceb1ff
commit 63b8aa334f
6 changed files with 26 additions and 90 deletions

View File

@ -161,7 +161,6 @@
<configuration>
<classes>
<param>com.baeldung.testing.assertj.custom.Person</param>
<param>com.baeldung.testing.assertj.custom.Car</param>
</classes>
</configuration>
</plugin>

View File

@ -1,20 +0,0 @@
package com.baeldung.testing.assertj.custom;
import static com.baeldung.testing.assertj.custom.CarAssert.assertThat;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class AssertJCarAssertUnitTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void whenCarTypeDoesNotMatch_thenIncorrect() {
thrown.expect(AssertionError.class);
thrown.expectMessage("Expected type SUV but was Sedan");
Car car = new Car("Sedan");
assertThat(car).hasType("SUV");
}
}

View File

@ -1,6 +1,7 @@
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;
@ -9,21 +10,35 @@ import org.junit.rules.ExpectedException;
public class AssertJCustomAssertionsUnitTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() {
thrown.expect(AssertionError.class);
thrown.expectMessage("Expected nickname John but did not have");
public void whenPersonNameMatches_thenCorrect() {
Person person = new Person("John Doe", 20);
person.addNickname("Nick");
assertThat(person).hasNickname("John");
assertThat(person).hasFullName("John Doe");
}
@Test
public void whenCarIsUsed_thenCorrect() {
public void whenPersonAgeLessThanEighteen_thenNotAdult() {
Person person = new Person("Jane Roe", 16);
Car car = new Car("SUV");
car.setOwner(person);
assertThat(car).isUsed();
try {
assertThat(person).isAdult();
fail();
} catch (AssertionError e) {
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected adult but was juvenile");
}
}
@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 nickname John but did not have");
}
}
}

View File

@ -1,26 +0,0 @@
package com.baeldung.testing.assertj.custom;
import static com.baeldung.testing.assertj.custom.PersonAssert.assertThat;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class AssertJPersonAssertUnitTest {
@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() {
thrown.expect(AssertionError.class);
thrown.expectMessage("Expected adult but was juvenile");
Person person = new Person("Jane Roe", 16);
assertThat(person).isAdult();
}
}

View File

@ -5,7 +5,5 @@ public class Assertions {
return new PersonAssert(actual);
}
public static CarAssert assertThat(Car actual) {
return new CarAssert(actual);
}
// static factory methods of other assertion classes
}

View File

@ -1,30 +0,0 @@
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;
}
}