BAEL-6023: Throwing an Exception If an Optional Is Present (#13116)
This commit is contained in:
parent
0e5e21f2ae
commit
c71ade5790
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,24 @@
|
|||
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";
|
||||
|
@ -33,7 +31,7 @@ public class UsesForOptionalUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() {
|
||||
void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() {
|
||||
|
||||
UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull();
|
||||
|
||||
|
@ -50,7 +48,7 @@ public class UsesForOptionalUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() {
|
||||
void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() {
|
||||
|
||||
UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional();
|
||||
|
||||
|
@ -61,4 +59,25 @@ public class UsesForOptionalUnitTest {
|
|||
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue