Friday 2 March 2018

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

No comments:

Post a Comment