junit

JUnit AssertThat Example

Photo of Alvin ReyesAlvin ReyesJuly 17th, 2015Last Updated: March 21st, 2019
0 17,484 2 minutes read

1. Introduction

TheassertThat is one of the JUnit methods from the Assert object that can be used to check if a specific value match to an expected one.

It primarily accepts 2 parameters. First one if the actual value and the second is a matcher object. It will then try to compare this two and returns a boolean result if its a match or not. Example of it’s usage as per below.
 
 
 

2. The Source Code

JUnitTestAssertThatAssertions.java

package com.areyes1.junitassertrue.sample;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;import org.junit.Before;import org.junit.Test;public class JUnitTestAssertThatAssertions {int totalNumberOfApplicants = 0;@Beforepublic void setData(){this.totalNumberOfApplicants = 9;}@Testpublic void testAssertThatEqual() {assertThat("123",is("123"));}@Testpublic void testAssertThatNotEqual() {assertThat(totalNumberOfApplicants,is(123));}@Testpublic void testAssertThatObject() {assertThat("123",isA(String.class));}@Testpublic void testAssertThatWMessage(){assertThat("They are not equal!","123",is("1234"));}}

The example shown above usesis andisA method from the hamcrest core to return a matcher object given the value. This is then used by theassertThat method that will then return a boolean result of the comparison.

Here’s the output:

Figure 1.0 AssertThat output
Figure 1.0 AssertThat output

Aside from the example above, there are far more methods that can be used to rigorously test one’s code. JUnits own hamcrest api has incorporate core, logical and object methods for this purpose.

2.1 Core Matchers

Before you start implementing your own Matcher’s, you should look at the core matchers that come with JUnit already. Here is a list of the matcher methods:

Core

  • any() Matches any object passed to it.
  • is() A matcher that checks if the given objects are equal.
  • describedAs() adds a description to the matcher

Logical

  • allOf() Takes an array of matchers and must all match the expected object.
  • anyOf() Takes an array of matcher and must match at least one of the matchers must report that it matches the target object.
  • not() Check if the object negates what was passed.

Object

The list above can all be used onassertThat method. It gives a wide range of possible scenarios to fully regress the extend of your algorithm, logic or processes of your application.

Want to be a JUnit Master ?
Subscribe to our newsletter and download theJUnitProgramming Cookbookright now!
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

3. Bonus: Custom Matchers for yourassertThat

You can actually create our own matcher and simply using that on your junit test case. See example of the custom matcher below.

CustomMatcher.java

package com.areyes1.junitassertthat.sample;import org.hamcrest.BaseMatcher;import org.hamcrest.Description;import org.hamcrest.Matcher;public class CustomMatcher {public static Matcher matches(final Object expected){return new BaseMatcher() {protected Object expectedObject = expected;public boolean matches(Object item) {return expectedObject.equals(item);}public void describeTo(Description description) {description.appendText(expectedObject.toString());}};}}

We can then use that matcher as part of our Junit Test source.

JUnitTestAssertThatCustomMatcher.java

package com.areyes1.junitassertthat.sample;import static org.junit.Assert.*;import static com.areyes1.junitassertthat.sample.CustomMatcher.*;import static org.hamcrest.CoreMatchers.*;import java.util.ArrayList;import java.util.List;import org.junit.Before;import org.junit.Test;public class JUnitTestAssertThatCustomMatcher {ArrayList listOfValidStrings = new ArrayList();private String inputValue = new String("Hello");@Beforepublic void setData(){listOfValidStrings.add("object_1");listOfValidStrings.add("object_2");listOfValidStrings.add("object_3");}@Testpublic void testLogic(){assertThat(inputValue,matches("Hello"));}}

Here’s the output:

Figure 2.0 AssertThat output
Figure 2.0 AssertThat output

4. Download the Eclipse project

This was an example of JUnitassertThat

Download
You can download the full source code of this example here :junit-assertthat-example
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Alvin ReyesAlvin ReyesJuly 17th, 2015Last Updated: March 21st, 2019
0 17,484 2 minutes read
Photo of Alvin Reyes

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.