Skip to content

Commit 6a09ba4

Browse files
sanesaijzheaux
authored andcommitted
Check String is not empty
Issue: BAEL-2128
1 parent 4d27803 commit 6a09ba4

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

java-strings/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<artifactId>icu4j</artifactId>
5353
<version>${icu4j.version}</version>
5454
</dependency>
55+
<dependency>
56+
<groupId>com.google.guava</groupId>
57+
<artifactId>guava</artifactId>
58+
<version>${guava.version}</version>
59+
</dependency>
5560

5661
<dependency>
5762
<groupId>com.vdurmont</groupId>
@@ -92,6 +97,7 @@
9297
<assertj.version>3.6.1</assertj.version>
9398
<jmh-core.version>1.19</jmh-core.version>
9499
<icu4j.version>61.1</icu4j.version>
100+
<guava.version>26.0-jre</guava.version>
95101
</properties>
96102

97103
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.string;
2+
3+
import static org.hamcrest.CoreMatchers.not;
4+
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
5+
import static org.hamcrest.text.IsEmptyString.isEmptyString;
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertNotEquals;
8+
import static org.junit.Assert.assertNotSame;
9+
import static org.junit.Assert.assertThat;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import org.apache.commons.lang3.StringUtils;
13+
import org.assertj.core.api.Assertions;
14+
import org.junit.Test;
15+
16+
import com.google.common.base.Strings;
17+
18+
public class StringEmptyUnitTest {
19+
20+
private String text = "baeldung";
21+
22+
@Test
23+
public void givenAString_whenCheckedForEmptyUsingJunit_shouldAssertSuccessfully() {
24+
assertTrue(!text.isEmpty());
25+
assertFalse(text.isEmpty());
26+
assertNotEquals("", text);
27+
assertNotSame("", text);
28+
}
29+
30+
@Test
31+
public void givenAString_whenCheckedForEmptyUsingHamcrest_shouldAssertSuccessfully() {
32+
assertThat(text, not(isEmptyString()));
33+
assertThat(text, not(isEmptyOrNullString()));
34+
}
35+
36+
@Test
37+
public void givenAString_whenCheckedForEmptyUsingCommonsLang_shouldAssertSuccessfully() {
38+
assertTrue(StringUtils.isNotBlank(text));
39+
}
40+
41+
@Test
42+
public void givenAString_whenCheckedForEmptyUsingAssertJ_shouldAssertSuccessfully() {
43+
Assertions.assertThat(text).isNotEmpty();
44+
}
45+
46+
@Test
47+
public void givenAString_whenCheckedForEmptyUsingGuava_shouldAssertSuccessfully() {
48+
assertFalse(Strings.isNullOrEmpty(text));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)