Monday, 5 March 2018

Hamcrest: stringContainsInOrder: Matches a string against all the substrings

By using stringContainsInOrder, you can create a matcher that checks whether the actual result contains the specified Strings in this sequence

Ex
List<String> subStrs1 = Arrays.asList("How", "are", "you");
assertThat("sentence must contain subStrs in order :", sentence, stringContainsInOrder(subStrs1));

Find the below working application.

TestApp.java

package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String sentence = "Hello Shiney, How are you";
  List<String> subStrs1 = Arrays.asList("How", "are", "you");
  List<String> subStrs2 = Arrays.asList("Hello", "you");
  List<String> subStrs3 = Arrays.asList("Shiney", "are", "you");

  /* Checks that the actual result contains the specified Strings in this sequence */
  assertThat("sentence must contain subStrs in order :", sentence, stringContainsInOrder(subStrs1));
  assertThat("sentence must contain subStrs in order :", sentence, stringContainsInOrder(subStrs2));
  assertThat("sentence must contain subStrs in order :", sentence, stringContainsInOrder(subStrs3));

 }
}




Previous                                                 Next                                                 Home

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

Hamcrest: isEmptyString: Match if the string is empty

By using isEmptyString method, you can create a matcher that matches when the examined string has zero length.

Ex
String str = "";
assertThat("str should be empty", str, isEmptyString());

Find the below working application.

TestApp.java
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyString;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String str = "";

  assertThat("str should be empty", str, isEmptyString());
 }
}




Previous                                                 Next                                                 Home

Hamcrest: equalToIgnoringWhiteSpace: check equality of strings by ignoring case and white spaces

By using 'equalToIgnoringWhiteSpace' method, you can create a matcher of string that matches when the examined string is equal to the specified expected string, by ignoring case and whitespaces.

While matching the strings, below rules are applied.

         1. All leading and trailing whitespace of both the expectedString and the examined string are ignored
         2. Any remaining whitespace, appearing within either string, is collapsed to a single space before comparison
        
Ex
String str = "Hello World";
assertThat("comparing strings by ignoring case ", str, equalToIgnoringWhiteSpace("Hello       World"));

Find the below working application.

TestApp.java
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String str = "Hello World";

  assertThat("comparing strings by ignoring case ", str, equalToIgnoringWhiteSpace("Hello       World"));
  assertThat("comparing strings by ignoring case ", str, equalToIgnoringWhiteSpace("  Hello World "));
 }
}




Previous                                                 Next                                                 Home

Tuesday, 27 February 2018

Hamcrest: equalToIgnoringCase: check equality of strings by ignoring case

By using 'equalToIgnoringCase' method, you can create a matcher of string that matches when the examined string is equal to the specified expected string, ignoring case.

Ex
String str = "abrakadabra";
assertThat("comparing strings by ignoring case ", str, equalToIgnoringCase("ABRAKADABRA"));

Find the below working application.

TestApp.java
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalToIgnoringCase;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String str = "abrakadabra";

  assertThat("comparing strings by ignoring case ", str, equalToIgnoringCase("ABRAKADABRA"));
  assertThat("comparing strings by ignoring case ", str, equalToIgnoringCase("ABRAKAdabra"));
  assertThat("comparing strings by ignoring case ", str, equalToIgnoringCase("abraKADABRA"));
  assertThat("comparing strings by ignoring case ", str, equalToIgnoringCase("ABRAkaDABRA"));

 }
}




Previous                                                 Next                                                 Home

Hamcrest: endsWith: Check whether string ends with given string

By using 'endsWith' method, you can create a matcher that matches if the examined string ends with the specified string.

Ex
assertThat("str should ends with 'd'", str, endsWith("d"));

Find the below working example

TestApp.java
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;

import org.junit.Test;

public class TestApp {

 @Test
 public void containsString_testWithDiffScenarios() {
  String str = "Hello World";

  assertThat("str should ends with 'd'", str, endsWith("d"));
  assertThat("str should ends with 'ld'", str, endsWith("ld"));
  assertThat("str should ends with 'rld'", str, endsWith("rld"));
  assertThat("str should ends with 'orld'", str, endsWith("orld"));
  assertThat("str should ends with 'World'", str, endsWith("World"));

 }
}




Previous                                                 Next                                                 Home

Hamcrest: startsWith: Check whether string starts with given string

By using 'startsWith' method, you can create a matcher that matches if the examined string starts with the specified string.

Ex
String str = "Hello World";
assertThat("str should starts with 'H'", str, startsWith("H"));

Find the below working application.

TestApp.java
package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;

import org.junit.Test;

public class TestApp {

 @Test
 public void containsString_testWithDiffScenarios() {
  String str = "Hello World";

  assertThat("str should starts with 'H'", str, startsWith("H"));
  assertThat("str should starts with 'He'", str, startsWith("He"));
  assertThat("str should starts with 'Hel'", str, startsWith("Hel"));
  assertThat("str should starts with 'Hell'", str, startsWith("Hell"));
  assertThat("str should starts with 'Hello'", str, startsWith("Hello"));

 }
}





Previous                                                 Next                                                 Home