Skip to content

Commit 4d87c77

Browse files
ngocnhan-tran1996sdeleuze
authored andcommitted
Add test cases for ObjectUtils#unwrapOptional
See spring-projectsgh-33618
1 parent fc8bd64 commit 4d87c77

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Rick Evans
6363
* @author Sam Brannen
6464
* @author Hyunjin Choi
65+
* @author Ngoc Nhan
6566
*/
6667
class ObjectUtilsTests {
6768

@@ -1114,4 +1115,22 @@ private static String prefix(Class<?> clazz) {
11141115
}
11151116
}
11161117

1118+
@Test
1119+
void unwrapOptional() {
1120+
1121+
assertThat(ObjectUtils.unwrapOptional(null)).isNull();
1122+
assertThat(ObjectUtils.unwrapOptional(Optional.empty())).isNull();
1123+
assertThat(ObjectUtils.unwrapOptional(Optional.of("some value"))).isEqualTo("some value");
1124+
1125+
Optional<Optional<Object>> nestedEmptyOptional = Optional.of(Optional.empty());
1126+
assertThatIllegalArgumentException()
1127+
.isThrownBy(() -> ObjectUtils.unwrapOptional(nestedEmptyOptional))
1128+
.withMessage("Multi-level Optional usage not supported");
1129+
1130+
Optional<Optional<String>> nestedStringOptional = Optional.of(Optional.of("some value"));
1131+
assertThatIllegalArgumentException()
1132+
.isThrownBy(() -> ObjectUtils.unwrapOptional(nestedStringOptional))
1133+
.withMessage("Multi-level Optional usage not supported");
1134+
}
1135+
11171136
}

0 commit comments

Comments
 (0)