|
| 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