By using isEmptyOrNullString, you can create a matcher that matches when the examined String is null, or has zero length.
Ex
String str1 = "";
String str2 = null;
assertThat("str1 should be empty", str1, isEmptyOrNullString());
assertThat("str2 should be null", str2, isEmptyOrNullString());
Find the below working application.
TestApp.java
package com.sample.tests;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import org.junit.Test;
public class TestApp {
@Test
public void testmethod() {
String str1 = "";
String str2 = null;
assertThat("str1 should be empty", str1, isEmptyOrNullString());
assertThat("str2 should be null", str2, isEmptyOrNullString());
}
}
Previous Next Home
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here