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