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:
parent
c209eecd5e
commit
8ac53f70c4
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue