From 54aee18a7f1ea088a45c5741b4b7ad88c4424cc3 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sat, 27 Apr 2024 11:56:24 +0200 Subject: [PATCH] BAEL-7783: Assert properties of custom exceptions with catchThrowableOfType in AssertJ (#16442) --- .../exceptions/CityNotFoundException.java | 22 +++++++++++++++++++ .../assertj/exceptions/CityUtils.java | 17 ++++++++++++++ .../exceptions/Java8StyleAssertions.java | 22 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityNotFoundException.java create mode 100644 testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityUtils.java diff --git a/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityNotFoundException.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityNotFoundException.java new file mode 100644 index 0000000000..e233ee737a --- /dev/null +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityNotFoundException.java @@ -0,0 +1,22 @@ +package com.baeldung.assertj.exceptions; + +public class CityNotFoundException extends RuntimeException { + + private String city; + private String message; + + CityNotFoundException(String city, String message) { + this.city = city; + this.message = message; + } + + public String getCity() { + return city; + } + + @Override + public String getMessage() { + return message; + } + +} diff --git a/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityUtils.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityUtils.java new file mode 100644 index 0000000000..9e9544a2a1 --- /dev/null +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/exceptions/CityUtils.java @@ -0,0 +1,17 @@ +package com.baeldung.assertj.exceptions; + +import java.util.Arrays; +import java.util.List; + +public final class CityUtils { + + private static final List CITIES = Arrays.asList("Tamassint", "London", "Madrid", "New york"); + + public static String search(String searchedCity) { + return CITIES.stream() + .filter(searchedCity::equals) + .findFirst() + .orElseThrow(() -> new CityNotFoundException(searchedCity, "The specified city is not found")); + } + +} diff --git a/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java index 4d4e2aedfc..54e41edf92 100644 --- a/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java @@ -1,13 +1,15 @@ package com.baeldung.assertj.exceptions; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.Assertions.catchThrowableOfType; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; + import org.junit.Test; public class Java8StyleAssertions { @@ -63,4 +65,22 @@ public class Java8StyleAssertions { .hasMessageContaining("/ by zero"); } + + @Test + public void whenUsingCatchThrowableOfType_thenAssertField() { + String givenCity = "Paris"; + CityNotFoundException exception = catchThrowableOfType(() -> CityUtils.search(givenCity), CityNotFoundException.class); + + assertThat(exception.getCity()).isEqualTo(givenCity); + assertThat(exception.getMessage()).isEqualTo("The specified city is not found"); + } + + @Test + public void whenUsingAssertThatThrownBy_thenAssertField() { + String givenCity = "Geneva"; + assertThatThrownBy(() -> CityUtils.search(givenCity)).isInstanceOf(CityNotFoundException.class) + .extracting("city") + .isEqualTo(givenCity); + } + }