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

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 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";
ListString> subStrs1 = Arrays.asList("How", "are", "you");
ListString> subStrs2 = Arrays.asList("Hello", "you");
ListString> 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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Hamcrest: stringContainsInOrder: Matches a string against all the substrings

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×