-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathCoolOptionalTest.java
More file actions
24 lines (19 loc) · 858 Bytes
/
CoolOptionalTest.java
File metadata and controls
24 lines (19 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package javaprogramming.commonmistakes.java8;
import org.junit.Test;
import java.util.Optional;
import java.util.OptionalDouble;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
public class CoolOptionalTest {
@Test(expected = IllegalArgumentException.class)
public void optional() {
assertThat(Optional.of(1).get(), is(1));
assertThat(Optional.ofNullable(null).orElse("A"), is("A"));
assertFalse(OptionalDouble.empty().isPresent());
assertThat(Optional.of(1).map(Math::incrementExact).get(), is(2));
assertThat(Optional.of(1).filter(integer -> integer % 2 == 0).orElse(null), is(nullValue()));
Optional.empty().orElseThrow(IllegalArgumentException::new);
}
}