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> <configuration>
<classes> <classes>
<param>com.baeldung.testing.assertj.custom.Person</param> <param>com.baeldung.testing.assertj.custom.Person</param>
<param>com.baeldung.testing.assertj.custom.Car</param>
</classes> </classes>
</configuration> </configuration>
</plugin> </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; package com.baeldung.testing.assertj.custom;
import static com.baeldung.testing.assertj.custom.Assertions.assertThat; import static com.baeldung.testing.assertj.custom.Assertions.assertThat;
import static org.junit.Assert.fail;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -11,19 +12,33 @@ public class AssertJCustomAssertionsUnitTest {
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Test @Test
public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { public void whenPersonNameMatches_thenCorrect() {
thrown.expect(AssertionError.class);
thrown.expectMessage("Expected nickname John but did not have");
Person person = new Person("John Doe", 20); Person person = new Person("John Doe", 20);
person.addNickname("Nick"); assertThat(person).hasFullName("John Doe");
assertThat(person).hasNickname("John");
} }
@Test @Test
public void whenCarIsUsed_thenCorrect() { public void whenPersonAgeLessThanEighteen_thenNotAdult() {
Person person = new Person("Jane Roe", 16); Person person = new Person("Jane Roe", 16);
Car car = new Car("SUV");
car.setOwner(person); try {
assertThat(car).isUsed(); 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); return new PersonAssert(actual);
} }
public static CarAssert assertThat(Car actual) { // static factory methods of other assertion classes
return new CarAssert(actual);
}
} }

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;
}
}