Tuesday 27 February 2018

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

No comments:

Post a Comment