Merge pull request #10574 from kwoyke/JAVA-4669

JAVA-4669: Code cleanup for UsingOptional
This commit is contained in:
Loredana Crusoveanu 2021-03-21 15:44:44 +02:00 committed by GitHub
commit 45764829f7
2 changed files with 6 additions and 9 deletions

View File

@ -38,8 +38,8 @@ public class UsingOptional {
.flatMap(list -> list.stream().findFirst()); .flatMap(list -> list.stream().findFirst());
} }
private Optional<List> getOptionalList() { private Optional<List<String>> getOptionalList() {
return Optional.ofNullable(getList()); return Optional.of(getList());
} }
private String doSomething(boolean processed) { private String doSomething(boolean processed) {

View File

@ -19,22 +19,22 @@ class UsingOptionalUnitTest {
} }
@Test @Test
public void whenArgIsFalse_thenReturnEmptyResponse() { void whenArgIsFalse_thenReturnEmptyResponse() {
Optional<Object> result = dataObject.process(false); Optional<Object> result = dataObject.process(false);
assertFalse(result.isPresent()); assertFalse(result.isPresent());
} }
@Test @Test
public void whenArgIsTrue_thenReturnValidResponse() { void whenArgIsTrue_thenReturnValidResponse() {
Optional<Object> result = dataObject.process(true); Optional<Object> result = dataObject.process(true);
assertTrue(result.isPresent()); assertTrue(result.isPresent());
} }
@Test @Test
public void whenArgIsFalse_thenChainResponseAndThrowException() { void whenArgIsFalse_thenChainResponseAndThrowException() {
assertThrows(Exception.class, () -> dataObject.process(false).orElseThrow(() -> new Exception())); assertThrows(Exception.class, () -> dataObject.process(false).orElseThrow(Exception::new));
} }
@Test() @Test()
@ -51,7 +51,4 @@ class UsingOptionalUnitTest {
void whenOptionalListFirst_returnsEmptyOptional() { void whenOptionalListFirst_returnsEmptyOptional() {
assertFalse(dataObject.optionalListFirst().isPresent()); assertFalse(dataObject.optionalListFirst().isPresent());
} }
} }