Move optional to java-8-core (#5105)

This commit is contained in:
Kacper
2018-08-29 15:51:41 +02:00
committed by maibin
parent 9028707a89
commit c4cb6eec73
3 changed files with 11 additions and 18 deletions

View File

@@ -1,16 +1,10 @@
package com.baeldung.throwsexception;
import javax.annotation.Nullable;
import java.sql.SQLException;
import java.util.List;
public class PersonRepository {
@Nullable
public String findNameById(String id) {
return id == null ? null : "Name";
}
public List<String> findAll() throws SQLException {
throw new SQLException();
}

View File

@@ -1,53 +0,0 @@
package com.baeldung.throwsexception;
import org.junit.Test;
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.assertThrows;
public class PersonRepositoryUnitTest {
PersonRepository personRepository = new PersonRepository();
@Test
public void whenIdIsNull_thenExceptionIsThrown() {
assertThrows(IllegalArgumentException.class,
() ->
Optional
.ofNullable(personRepository.findNameById(null))
.orElseThrow(IllegalArgumentException::new));
}
@Test
public void whenIdIsNonNull_thenNoExceptionIsThrown() {
assertAll(
() ->
Optional
.ofNullable(personRepository.findNameById("id"))
.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
public void whenIdIsNonNull_thenShouldReturnNameUpperCase() throws Exception {
String name = Optional
.ofNullable(personRepository.findNameById("id"))
.map(String::toUpperCase)
.orElseThrow(Exception::new);
assertEquals("NAME", name);
}
}