BAEL-2122 Optional or else throw (#5047)
* Throw and throws in Java * BAEL-2122 | throw exception in optional * BAEL-2122 | optional orelsethrow
This commit is contained in:
parent
2779300352
commit
9b343be45e
@ -8,7 +8,7 @@ public class PersonRepository {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public String findNameById(String id) {
|
public String findNameById(String id) {
|
||||||
return id == null ? null : "example-name";
|
return id == null ? null : "Name";
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> findAll() throws SQLException {
|
public List<String> findAll() throws SQLException {
|
||||||
|
@ -4,6 +4,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
@ -29,4 +30,14 @@ public class PersonRepositoryUnitTest {
|
|||||||
.orElseThrow(Exception::new));
|
.orElseThrow(Exception::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenIdIsNonNull_thenShouldReturnNameUpperCase() throws Exception {
|
||||||
|
String name = Optional
|
||||||
|
.ofNullable(personRepository.findNameById("id"))
|
||||||
|
.map(String::toUpperCase)
|
||||||
|
.orElseThrow(Exception::new);
|
||||||
|
|
||||||
|
assertEquals("NAME", name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user