From 4e63e54f2f527df45ceaed96ac7c4fbbc9cb3f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Kesz=C3=B6cze?= Date: Thu, 21 Sep 2023 12:01:33 +0200 Subject: [PATCH 1/3] make error handling use AssertJ for exceptions --- .../src/test/java/ErrorHandlingTest.java | 89 +++++++------------ 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java index 4f8947571..c1c47c800 100644 --- a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java +++ b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java @@ -1,8 +1,7 @@ -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import org.junit.Ignore; import org.junit.Test; @@ -15,105 +14,83 @@ public class ErrorHandlingTest { @Test public void testThrowIllegalArgumentException() { - assertThrows( - IllegalArgumentException.class, - errorHandling::handleErrorByThrowingIllegalArgumentException); + assertThatExceptionOfType(Exception.class). + isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException()); } @Ignore("Remove to run test") @Test public void testThrowIllegalArgumentExceptionWithDetailMessage() { - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> errorHandling - .handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage( - "This is the detail message.")); - - assertThat(expected).hasMessage("This is the detail message."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage( + "This is the detail message.")) + .withMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowAnyCheckedException() { - Exception expected = - assertThrows( - Exception.class, - errorHandling::handleErrorByThrowingAnyCheckedException); - assertThat(expected).isNotInstanceOf(RuntimeException.class); + assertThatExceptionOfType(Exception.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedException()) + .isNotInstanceOf(RuntimeException.class); } @Ignore("Remove to run test") @Test public void testThrowAnyCheckedExceptionWithDetailMessage() { - Exception expected = - assertThrows( - Exception.class, - () -> errorHandling - .handleErrorByThrowingAnyCheckedExceptionWithDetailMessage( - "This is the detail message.")); - assertThat(expected).isNotInstanceOf(RuntimeException.class); - assertThat(expected).hasMessage("This is the detail message."); + assertThatExceptionOfType(Exception.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage( + "This is the detail message.")) + .isNotInstanceOf(RuntimeException.class) + .withMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowAnyUncheckedException() { - assertThrows( - RuntimeException.class, - errorHandling::handleErrorByThrowingAnyUncheckedException); + assertThatExceptionOfType(RuntimeException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedException()); } @Ignore("Remove to run test") @Test public void testThrowAnyUncheckedExceptionWithDetailMessage() { - RuntimeException expected = - assertThrows( - RuntimeException.class, - () -> errorHandling - .handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage( - "This is the detail message.")); - assertThat(expected).hasMessage("This is the detail message."); + assertThatExceptionOfType(RuntimeException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage( + "This is the detail message.")) + .withMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowCustomCheckedException() { - assertThrows( - CustomCheckedException.class, - errorHandling::handleErrorByThrowingCustomCheckedException); + assertThatExceptionOfType(CustomCheckedException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedException()); } @Ignore("Remove to run test") @Test public void testThrowCustomCheckedExceptionWithDetailMessage() { - CustomCheckedException expected = - assertThrows( - CustomCheckedException.class, - () -> errorHandling - .handleErrorByThrowingCustomCheckedExceptionWithDetailMessage( - "This is the detail message.")); - assertThat(expected).hasMessage("This is the detail message."); + assertThatExceptionOfType(CustomCheckedException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage( + "This is the detail message.")) + .withMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowCustomUncheckedException() { - assertThrows( - CustomUncheckedException.class, - errorHandling::handleErrorByThrowingCustomUncheckedException); + assertThatExceptionOfType(CustomUncheckedException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedException()); } @Ignore("Remove to run test") @Test public void testThrowCustomUncheckedExceptionWithDetailMessage() { - CustomUncheckedException expected = - assertThrows( - CustomUncheckedException.class, - () -> errorHandling - .handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage( - "This is the detail message.")); - assertThat(expected).hasMessage("This is the detail message."); + assertThatExceptionOfType(CustomUncheckedException.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage( + "This is the detail message.")) + .withMessage("This is the detail message."); } @Ignore("Remove to run test") From 06e9d8090bcee944c178c1ea2975079f0d373836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Kesz=C3=B6cze?= Date: Wed, 27 Sep 2023 10:40:43 +0200 Subject: [PATCH 2/3] Adhere to coding style --- .../error-handling/src/test/java/ErrorHandlingTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java index c1c47c800..2be6d579b 100644 --- a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java +++ b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java @@ -14,8 +14,8 @@ public class ErrorHandlingTest { @Test public void testThrowIllegalArgumentException() { - assertThatExceptionOfType(Exception.class). - isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException()); + assertThatExceptionOfType(Exception.class) + .isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException()); } @Ignore("Remove to run test") From cce178318ac88e27b611a7c5482ee2b8ae408d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Kesz=C3=B6cze?= Date: Wed, 27 Sep 2023 10:40:52 +0200 Subject: [PATCH 3/3] Completely switch to AssertJ --- .../src/test/java/ErrorHandlingTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java index 2be6d579b..7ed7c886c 100644 --- a/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java +++ b/exercises/practice/error-handling/src/test/java/ErrorHandlingTest.java @@ -1,7 +1,4 @@ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.*; import org.junit.Ignore; import org.junit.Test; @@ -97,11 +94,11 @@ public void testThrowCustomUncheckedExceptionWithDetailMessage() { @Test public void testReturnOptionalInstance() { Optional successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1"); - assertTrue(successfulResult.isPresent()); - assertEquals(1, (int) successfulResult.get()); + assertThat(successfulResult).isPresent().hasValue(1); Optional failureResult = errorHandling.handleErrorByReturningOptionalInstance("a"); - assertFalse(failureResult.isPresent()); + assertThat(failureResult).isNotPresent(); + } }