Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

HamCrest: isEmptyOrNullString: Match if the string is empty or null

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

Share the post

HamCrest: isEmptyOrNullString: Match if the string is empty or null

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×