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:
Kacper 2018-08-24 13:18:30 +02:00 committed by maibin
parent 2779300352
commit 9b343be45e
2 changed files with 12 additions and 1 deletions

View File

@ -8,7 +8,7 @@ public class PersonRepository {
@Nullable
public String findNameById(String id) {
return id == null ? null : "example-name";
return id == null ? null : "Name";
}
public List<String> findAll() throws SQLException {

View File

@ -4,6 +4,7 @@ import org.junit.Test;
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.assertThrows;
@ -29,4 +30,14 @@ public class PersonRepositoryUnitTest {
.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);
}
}