From 63b8aa334f89ab042e4203f99d032b03a1dd8b60 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Thu, 1 Feb 2018 11:00:51 +0700 Subject: [PATCH] Refactor BAEL-1515 --- testing-modules/testing/pom.xml | 1 - .../custom/AssertJCarAssertUnitTest.java | 20 ----------- .../AssertJCustomAssertionsUnitTest.java | 35 +++++++++++++------ .../custom/AssertJPersonAssertUnitTest.java | 26 -------------- .../testing/assertj/custom/Assertions.java | 4 +-- .../testing/assertj/custom/CarAssert.java | 30 ---------------- 6 files changed, 26 insertions(+), 90 deletions(-) delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 6f185d3b4c..c76045380b 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -161,7 +161,6 @@ com.baeldung.testing.assertj.custom.Person - com.baeldung.testing.assertj.custom.Car diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java deleted file mode 100644 index d438cc42f6..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java +++ /dev/null @@ -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"); - } -} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java index 8b800de3db..f9b5bad039 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -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"); + } } } diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java deleted file mode 100644 index ab421915af..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java +++ /dev/null @@ -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(); - } -} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java index 5c72eb6d05..fcffb8fc6c 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java @@ -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 } diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java deleted file mode 100644 index 413c2d3e12..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.testing.assertj.custom; - -import org.assertj.core.api.AbstractAssert; - -public class CarAssert extends AbstractAssert { - - 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; - } -}