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();
|
PersonRepository personRepository = new PersonRepository();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenIdIsNull_thenExceptionIsThrown() throws Exception {
|
public void whenIdIsNull_thenExceptionIsThrown() {
|
||||||
assertThrows(Exception.class,
|
assertThrows(IllegalArgumentException.class,
|
||||||
() ->
|
() ->
|
||||||
Optional
|
Optional
|
||||||
.ofNullable(personRepository.findNameById(null))
|
.ofNullable(personRepository.findNameById(null))
|
||||||
.orElseThrow(Exception::new));
|
.orElseThrow(IllegalArgumentException::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception {
|
public void whenIdIsNonNull_thenNoExceptionIsThrown() {
|
||||||
assertAll(
|
assertAll(
|
||||||
() ->
|
() ->
|
||||||
Optional
|
Optional
|
||||||
.ofNullable(personRepository.findNameById("id"))
|
.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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue