Move optional to java-8-core (#5105)
This commit is contained in:
parent
9028707a89
commit
c4cb6eec73
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.optional;
|
||||||
|
|
||||||
|
public class PersonRepository {
|
||||||
|
|
||||||
|
public String findNameById(String id) {
|
||||||
|
return id == null ? null : "Name";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.throwsexception;
|
package com.baeldung.optional;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Optional;
|
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.assertAll;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class PersonRepositoryUnitTest {
|
public class PersonRepositoryUnitTest {
|
||||||
@ -40,14 +40,4 @@ public class PersonRepositoryUnitTest {
|
|||||||
assertEquals("NAME", name);
|
assertEquals("NAME", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenIdIsNonNull_thenShouldReturnNameUpperCase() throws Exception {
|
|
||||||
String name = Optional
|
|
||||||
.ofNullable(personRepository.findNameById("id"))
|
|
||||||
.map(String::toUpperCase)
|
|
||||||
.orElseThrow(Exception::new);
|
|
||||||
|
|
||||||
assertEquals("NAME", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,16 +1,10 @@
|
|||||||
package com.baeldung.throwsexception;
|
package com.baeldung.throwsexception;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PersonRepository {
|
public class PersonRepository {
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public String findNameById(String id) {
|
|
||||||
return id == null ? null : "Name";
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> findAll() throws SQLException {
|
public List<String> findAll() throws SQLException {
|
||||||
throw new SQLException();
|
throw new SQLException();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user