BAEL-6023: Throwing an Exception If an Optional Is Present (#13116)

This commit is contained in:
Azhwani 2022-12-21 17:18:46 +01:00 committed by GitHub
parent 0e5e21f2ae
commit c71ade5790
3 changed files with 55 additions and 16 deletions

View File

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

View File

@ -18,4 +18,13 @@ public class UserRepositoryWithOptional {
return Optional.empty(); return Optional.empty();
} }
public void throwExceptionWhenUserIsPresent(String id) {
this.findById(id)
.ifPresent(user -> {
throw new UserFoundException("User with ID : " + user.getId() + " is found");
});
}
} }

View File

@ -1,39 +1,37 @@
package com.baeldung.optionaluses; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
public class UsesForOptionalUnitTest { import org.junit.jupiter.api.Test;
class UsesForOptionalUnitTest {
@Test @Test
public void givenNonExistentUserId_whenSearchForUser_andNoNullCheck_thenThrowException() { void givenNonExistentUserId_whenSearchForUser_andNoNullCheck_thenThrowException() {
UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull(); UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull();
String nonExistentUserId = "4"; String nonExistentUserId = "4";
assertThrows(NullPointerException.class, () -> { assertThrows(NullPointerException.class, () -> System.out.println("User name: " + userRepositoryWithNull.findById(nonExistentUserId).getName()));
System.out.println("User name: " + userRepositoryWithNull.findById(nonExistentUserId)
.getName());
});
} }
@Test @Test
public void givenNonExistentUserId_whenSearchForUser_thenOptionalShouldBeTreatedProperly() { void givenNonExistentUserId_whenSearchForUser_thenOptionalShouldBeTreatedProperly() {
UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional();
String nonExistentUserId = "4"; String nonExistentUserId = "4";
String userName = userRepositoryWithOptional.findById(nonExistentUserId) String userName = userRepositoryWithOptional.findById(nonExistentUserId)
.orElse(new User("0", "admin")) .orElse(new User("0", "admin"))
.getName(); .getName();
assertEquals("admin", userName); assertEquals("admin", userName);
} }
@Test @Test
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() { void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() {
UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull(); UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull();
@ -50,15 +48,36 @@ public class UsesForOptionalUnitTest {
} }
@Test @Test
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() { void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() {
UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional(); UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional();
String upperCasedName = userRepositoryWithOptional.findById("2") String upperCasedName = userRepositoryWithOptional.findById("2")
.filter(u -> u.getName().startsWith("M")) .filter(u -> u.getName().startsWith("M"))
.map(u -> u.getName().toUpperCase()) .map(u -> u.getName().toUpperCase())
.orElse(""); .orElse("");
assertEquals("MARIA", upperCasedName); 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));
}
} }