Optional or else throw (#5088)

* Throw and throws in Java

* BAEL-2122 | throw exception in optional

* BAEL-2122 | optional orelsethrow

* fix test
This commit is contained in:
Kacper 2018-08-27 22:21:33 +02:00 committed by maibin
parent c209eecd5e
commit 8ac53f70c4
1 changed files with 15 additions and 5 deletions

View File

@ -13,21 +13,31 @@ public class PersonRepositoryUnitTest {
PersonRepository personRepository = new PersonRepository();
@Test
public void whenIdIsNull_thenExceptionIsThrown() throws Exception {
assertThrows(Exception.class,
public void whenIdIsNull_thenExceptionIsThrown() {
assertThrows(IllegalArgumentException.class,
() ->
Optional
.ofNullable(personRepository.findNameById(null))
.orElseThrow(Exception::new));
.orElseThrow(IllegalArgumentException::new));
}
@Test
public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception {
public void whenIdIsNonNull_thenNoExceptionIsThrown() {
assertAll(
() ->
Optional
.ofNullable(personRepository.findNameById("id"))
.orElseThrow(Exception::new));
.orElseThrow(RuntimeException::new));
}
@Test
public void whenIdNonNull_thenReturnsNameUpperCase() {
String name = Optional
.ofNullable(personRepository.findNameById("id"))
.map(String::toUpperCase)
.orElseThrow(RuntimeException::new);
assertEquals("NAME", name);
}
@Test