File tree Expand file tree Collapse file tree
src/test/java/com/baeldung Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414 <version >1.0.0-SNAPSHOT</version >
1515 </parent >
1616
17+ <dependencies >
18+ <dependency >
19+ <groupId >com.google.guava</groupId >
20+ <artifactId >guava</artifactId >
21+ <version >${guava.version} </version >
22+ </dependency >
23+ </dependencies >
24+
1725 <build >
1826 <plugins >
1927 <plugin >
3139 <properties >
3240 <maven .compiler.source.version>11</maven .compiler.source.version>
3341 <maven .compiler.target.version>11</maven .compiler.target.version>
42+ <guava .version>27.1-jre</guava .version>
3443 </properties >
3544
3645</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung ;
2+
3+ import com .google .common .base .Strings ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+
7+ import java .util .Optional ;
8+ import java .util .function .Predicate ;
9+
10+ public class EmptyStringToEmptyOptionalUnitTest {
11+
12+ @ Test
13+ public void givenEmptyString_whenFilteringOnOptional_thenEmptyOptionalIsReturned () {
14+ String str = "" ;
15+ Optional <String > opt = Optional .ofNullable (str ).filter (s -> !s .isEmpty ());
16+ Assert .assertFalse (opt .isPresent ());
17+ }
18+
19+ @ Test
20+ public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned () {
21+ String str = "" ;
22+ Optional <String > opt = Optional .ofNullable (str ).filter (Predicate .not (String ::isEmpty ));
23+ Assert .assertFalse (opt .isPresent ());
24+ }
25+
26+ @ Test
27+ public void givenEmptyString_whenPassingResultOfEmptyToNullToOfNullable_thenEmptyOptionalIsReturned () {
28+ String str = "" ;
29+ Optional <String > opt = Optional .ofNullable (Strings .emptyToNull (str ));
30+ Assert .assertFalse (opt .isPresent ());
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments