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

No comments:

Post a Comment