From c71ade5790314e363f69254fc59ed408e06663ee Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Wed, 21 Dec 2022 17:18:46 +0100 Subject: [PATCH] BAEL-6023: Throwing an Exception If an Optional Is Present (#13116) --- .../optionaluses/UserFoundException.java | 11 ++++ .../UserRepositoryWithOptional.java | 9 ++++ .../optionaluses/UsesForOptionalUnitTest.java | 51 +++++++++++++------ 3 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserFoundException.java diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserFoundException.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserFoundException.java new file mode 100644 index 0000000000..d9dab9f05f --- /dev/null +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserFoundException.java @@ -0,0 +1,11 @@ +package com.baeldung.optionaluses; + +public class UserFoundException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public UserFoundException(String message) { + super(message); + } + +} diff --git a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserRepositoryWithOptional.java b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserRepositoryWithOptional.java index f31e35d85f..ee43addec7 100644 --- a/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserRepositoryWithOptional.java +++ b/core-java-modules/core-java-optional/src/main/java/com/baeldung/optionaluses/UserRepositoryWithOptional.java @@ -18,4 +18,13 @@ public class UserRepositoryWithOptional { return Optional.empty(); } + + public void throwExceptionWhenUserIsPresent(String id) { + + this.findById(id) + .ifPresent(user -> { + throw new UserFoundException("User with ID : " + user.getId() + " is found"); + }); + + } } diff --git a/core-java-modules/core-java-optional/src/test/java/com/baeldung/optionaluses/UsesForOptionalUnitTest.java b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optionaluses/UsesForOptionalUnitTest.java index d0f92be097..5c63987193 100644 --- a/core-java-modules/core-java-optional/src/test/java/com/baeldung/optionaluses/UsesForOptionalUnitTest.java +++ b/core-java-modules/core-java-optional/src/test/java/com/baeldung/optionaluses/UsesForOptionalUnitTest.java @@ -1,39 +1,37 @@ package com.baeldung.optionaluses; -import org.junit.jupiter.api.Test; - +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -public class UsesForOptionalUnitTest { +import org.junit.jupiter.api.Test; + +class UsesForOptionalUnitTest { @Test - public void givenNonExistentUserId_whenSearchForUser_andNoNullCheck_thenThrowException() { + void givenNonExistentUserId_whenSearchForUser_andNoNullCheck_thenThrowException() { UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull(); String nonExistentUserId = "4"; - assertThrows(NullPointerException.class, () -> { - System.out.println("User name: " + userRepositoryWithNull.findById(nonExistentUserId) - .getName()); - }); + assertThrows(NullPointerException.class, () -> System.out.println("User name: " + userRepositoryWithNull.findById(nonExistentUserId).getName())); } @Test - public void givenNonExistentUserId_whenSearchForUser_thenOptionalShouldBeTreatedProperly() { + void givenNonExistentUserId_whenSearchForUser_thenOptionalShouldBeTreatedProperly() { UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); String nonExistentUserId = "4"; String userName = userRepositoryWithOptional.findById(nonExistentUserId) - .orElse(new User("0", "admin")) - .getName(); + .orElse(new User("0", "admin")) + .getName(); assertEquals("admin", userName); } @Test - public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() { + void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() { UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull(); @@ -50,15 +48,36 @@ public class UsesForOptionalUnitTest { } @Test - public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() { + void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() { UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); String upperCasedName = userRepositoryWithOptional.findById("2") - .filter(u -> u.getName().startsWith("M")) - .map(u -> u.getName().toUpperCase()) - .orElse(""); + .filter(u -> u.getName().startsWith("M")) + .map(u -> u.getName().toUpperCase()) + .orElse(""); assertEquals("MARIA", upperCasedName); } + + @Test + void givenExistentUserId_whenSearchForUser_thenThrowException() { + + final UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); + String existentUserId = "2"; + + assertThrows(UserFoundException.class, () -> userRepositoryWithOptional.throwExceptionWhenUserIsPresent(existentUserId)); + + } + + @Test + void givenNonExistentUserId_whenSearchForUser_thenDoNotThrowException() { + + final UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); + String nonExistentUserId = "8"; + + assertDoesNotThrow(() -> userRepositoryWithOptional.throwExceptionWhenUserIsPresent(nonExistentUserId)); + + } + }