DEV Community

André Maré
André Maré

Posted on • Originally published at code2bits.com on

Writing Assertions for String Objects using Hamcrest and JUnit

In this post, I will show you how to write assertions for String Objects making use of the Hamcrest Library when writing your JUnit testcases. The post provides some useful examples, and some important tips I’ve learned from experience.

Requirements

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

This post forms part of a multi-part series on how to use the Hamcrest java library to write assertions as part of your unit tests.

  • Writing Assertions for String Objects using Hamcrest and JUnit
  • Writing Assertions for Collections using Hamcrest and JUnit

Introduction

Hamcrest is a framework that assists the writing of unit testcases in the Java programming language. The name Hamcrest is an anagram for “matchers”. Hamcrest is a library of matchers, which can be combined to create flexible expressions of intent in tests, thus allowing ‘match’ rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock. Hamcrest has been included in JUnit 4 since 2012, but was omitted from Junit 5 in 2017.

Even though the assertion facilities provided by JUnit Jupiter are sufficient for many testing scenarios, there are times when more power and additional functionality such as matchers are desired or required. In such cases, the JUnit team recommends the use of third-party assertion libraries such as AssertJ, Hamcrest, Truth, etc. Developers are therefore free to use the assertion library of their choice. ~ JUnit 5 User Guide

Examples: Assertions for String Objects

This section contains a number of examples of how to make use of the Hamcrest Java Library to write assertions for String Objects as part of JUnit test cases. This is not suppose to be a complete list of all possible scenarios in which you can write assertions for String objects, but serves as a list of frequently used examples.

1. Assert that two String Objects are equal.

An example of a JUnit test method asserting that two String objects are equal using the ‘is’ Hamcrest matcher.

@Test 
public final void validateTwoStringsEqual() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "myTestValue"; 
    assertThat(actualValue, is(expectedValue)); 
}

2. Assert that two String Objects are equal.

An example of a JUnit test method asserting that two String objects are equal using the ‘equalTo’ Hamcrest matcher.

@Test 
public final void validateTwoStringsEqualTo() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "myTestValue"; 
    assertThat(actualValue, equalTo(expectedValue)); 
}

3. Assert that two String Objects are not equal.

An example of a JUnit test method asserting that two String objects are not equal using the ‘not’ and ‘equalTo’ Hamcrest matcher.

@Test 
public final void validateTwoStringsNotEqualTo() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "myTestValue"; 
    assertThat(actualValue, not(equalTo(expectedValue))); 
}

4. Assert that String is always matched.

An example of a JUnit test method asserting that a String object is always matched using the ‘anything’ Hamcrest matcher.

@Test 
public final void validateStringIsAnything() { 
    String actualValue = "myTestValue"; 
    assertThat(actualValue, anything()); 
}

5. Assert that String is always marched with specified Value

An example of a JUnit test method asserting that a String object is always matched using the ‘anything’ Hamcrest matcher but describes itself with the specified String.

@Test 
public final void validateStringIsAnyString() { 
    String actualValue = "myTestValue"; 
    String specifiedValue = "Anything"; 
    assertThat(actualValue, anything(specifiedValue)); 
}

6. Assert that String starts with an expected value.

An example of a JUnit test method asserting that a String object starts with an expected value using the ‘startsWith’ Hamcrest matcher.

@Test 
public final void validateStringStartsWith() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "my"; 
    assertThat(actualValue, startsWith(expectedValue)); 
}

7. Assert that String contains an expected value.

An example of a JUnit test method asserting that a String object contains an expected value using the ‘containsString’ Hamcrest matcher.

@Test 
public final void validateStringContainsString() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "Test"; 
    assertThat(actualValue, containsString(expectedValue)); 
}

8. Assert that String ends with an expected value.

An example of a JUnit test method asserting that a String object ends with an expected value using the ‘endsWith’ Hamcrest matcher.

@Test 
public final void validateStringEndsWithString() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "Value"; 
    assertThat(actualValue, endsWith(expectedValue)); 
}

9. Assert that String is equal to an expected value but ignores case.

An example of a JUnit test method asserting that a String object is equal to an expected value while ignoring case using the ‘equalToIgnoringCase’ Hamcrest matcher.

@Test 
public final void validateStringEqualToIgnoringCase() { 
    String actualValue = "myTestValue"; 
    String expectedValue = "mytestvalue"; 
    assertThat(actualValue, equalToIgnoringCase(expectedValue)); 
}

10. Assert that String is equal to an expected value but ignores white spaces.

An example of a JUnit test method asserting that a String object is equal to an expected value while ignoring white spaces using the ‘equalToIgnoringWhiteSpace’ Hamcrest matcher.

@Test 
public final void validateStringEqualToIgnoringWhiteSpace() { 
    String actualValue = "myTestValue"; 
    String expectedValue = " myTestValue "; 
    assertThat(actualValue, equalToIgnoringWhiteSpace(expectedValue)); 
}

11. Assert that String Object is empty.

An example of a JUnit test method asserting that a String object is empty using the ‘isEmptyString’ Hamcrest matcher.

@Test 
public final void validateStringIsEmptyString() { 
    String actualValue = ""; 
    assertThat(actualValue, isEmptyString()); 
}

12. Assert that String Object is empty.

An example of a JUnit test method asserting that a String object is empty using the ‘isEmptyOrNullString’ Hamcrest matcher.

@Test 
public final void validateStringIsEmptyOrNullString1() { 
    String actualValue = ""; 
    assertThat(actualValue, isEmptyOrNullString()); 
}

13. Assert that String Object is null.

An example of a JUnit test method asserting that a String object is null using the ‘isEmptyOrNullString’ Hamcrest matcher.

@Test 
public final void validateStringIsEmptyOrNullString2() { 
    String actualValue = null; 
    assertThat(actualValue, isEmptyOrNullString()); 
}

14. Assert that String contains expected value in order.

An example of a JUnit test method asserting that a String object contains the expected value in order using the ‘stringContainsInOrder’ Hamcrest matcher.

@Test 
public final void validateStringContainsInOrder() { 
    String actualValue = "ABCDEFGH"; 
    assertThat(actualValue, stringContainsInOrder(Arrays.asList("A", "B", "C"))); 
}

Summary

The examples listed in this post is not intended to be a complete list of how to make use of the Hamcrest Java Library to write assertions for String Objects. This post serves to illustrate some of the more frequent scenarios for writing assertions on String objects. For more information and more examples, please follow me on any of the different social media platforms and feel free to leave comments.

The post Writing Assertions for String Objects using Hamcrest and JUnit appeared first on Code2Bits.

Oldest comments (1)

Collapse
 
jenith_lalseta profile image
Jenith lalseta • Edited

Thanks for sharing. Its helpfull. I also need help to know how can I find difference between two hash and print difference between actual and expected using assertEquals