添加 Optional 类的测试文件和文章说明连接: https://www.ossez.com/t/java-8-optional/13964
This commit is contained in:
parent
dfd51f0723
commit
74a9a9ed0b
@ -3,7 +3,7 @@
|
|||||||
本模块中包含有关 Java 11 核心新增功能的的一些文章
|
本模块中包含有关 Java 11 核心新增功能的的一些文章
|
||||||
|
|
||||||
### 相关文章
|
### 相关文章
|
||||||
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
|
- [Java 8 开始新增的 Optional 类](https://www.ossez.com/t/java-8-optional/13964)
|
||||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||||
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
|
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
|
||||||
|
@ -76,16 +76,16 @@ public class OptionalUnitTest {
|
|||||||
// Condition Action With ifPresent()
|
// Condition Action With ifPresent()
|
||||||
@Test
|
@Test
|
||||||
public void givenOptional_whenIfPresentWorks_thenCorrect() {
|
public void givenOptional_whenIfPresentWorks_thenCorrect() {
|
||||||
Optional<String> opt = Optional.of("baeldung");
|
Optional<String> opt = Optional.of("HoneyMoose");
|
||||||
opt.ifPresent(name -> LOG.debug("{}", name.length()));
|
opt.ifPresent(name -> LOG.debug("{}", name.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// returning Value With get()
|
// returning Value With get()
|
||||||
@Test
|
@Test
|
||||||
public void givenOptional_whenGetsValue_thenCorrect() {
|
public void givenOptional_whenGetsValue_thenCorrect() {
|
||||||
Optional<String> opt = Optional.of("baeldung");
|
Optional<String> opt = Optional.of("HoneyMoose");
|
||||||
String name = opt.get();
|
String name = opt.get();
|
||||||
assertEquals("baeldung", name);
|
assertEquals("HoneyMoose", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NoSuchElementException.class)
|
@Test(expected = NoSuchElementException.class)
|
||||||
@ -96,13 +96,23 @@ public class OptionalUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
|
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
|
||||||
Optional<String> opt = Optional.of("Baeldung");
|
Optional<String> opt = Optional.of("HoneyMoose");
|
||||||
assertTrue(opt.isPresent());
|
assertTrue(opt.isPresent());
|
||||||
|
|
||||||
opt = Optional.ofNullable(null);
|
opt = Optional.ofNullable(null);
|
||||||
assertFalse(opt.isPresent());
|
assertFalse(opt.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This code only can be run in JDK 11
|
||||||
|
@Test
|
||||||
|
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected_JDK11() {
|
||||||
|
Optional<String> opt = Optional.of("Baeldung");
|
||||||
|
assertFalse(opt.isEmpty());
|
||||||
|
|
||||||
|
opt = Optional.ofNullable(null);
|
||||||
|
assertTrue(opt.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
// Conditional Return With filter()
|
// Conditional Return With filter()
|
||||||
@Test
|
@Test
|
||||||
public void whenOptionalFilterWorks_thenCorrect() {
|
public void whenOptionalFilterWorks_thenCorrect() {
|
||||||
@ -157,11 +167,11 @@ public class OptionalUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOptional_whenMapWorks_thenCorrect2() {
|
public void givenOptional_whenMapWorks_thenCorrect2() {
|
||||||
String name = "baeldung";
|
String name = "HoneyMoose";
|
||||||
Optional<String> nameOptional = Optional.of(name);
|
Optional<String> nameOptional = Optional.of(name);
|
||||||
|
|
||||||
int len = nameOptional.map(String::length).orElse(0);
|
int len = nameOptional.map(String::length).orElse(0);
|
||||||
assertEquals(8, len);
|
assertEquals(10, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -214,7 +224,6 @@ public class OptionalUnitTest {
|
|||||||
String nullName = null;
|
String nullName = null;
|
||||||
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
|
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
|
||||||
assertEquals("john", name);
|
assertEquals("john", name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -259,14 +268,5 @@ public class OptionalUnitTest {
|
|||||||
return "Default Value";
|
return "Default Value";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uncomment code when code base is compatible with Java 11
|
|
||||||
// @Test
|
|
||||||
// public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
|
|
||||||
// Optional<String> opt = Optional.of("Baeldung");
|
|
||||||
// assertFalse(opt.isEmpty());
|
|
||||||
//
|
|
||||||
// opt = Optional.ofNullable(null);
|
|
||||||
// assertTrue(opt.isEmpty());
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user