junit

JUnit AssertThat Example

1. Introduction

The assertThat 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;
	
	@Before
	public void setData(){
		this.totalNumberOfApplicants = 9;
	}
	
	@Test
	public void testAssertThatEqual() {
		assertThat("123",is("123"));
	}
	
	@Test
	public void testAssertThatNotEqual() {
		assertThat(totalNumberOfApplicants,is(123));
	}
	
	@Test
	public void testAssertThatObject() {
		assertThat("123",isA(String.class));
	}
	
	@Test
	public void testAssertThatWMessage(){
		assertThat("They are not equal!","123",is("1234"));
	}
}


The example shown above uses is and isA method from the hamcrest core to return a matcher object given the value. This is then used by the assertThat 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 on assertThat method. It gives a wide range of possible scenarios to fully regress the extend of your algorithm, logic or processes of your application.

3. Bonus: Custom Matchers for your assertThat

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");
	@Before
	public void setData(){
		listOfValidStrings.add("object_1");
		listOfValidStrings.add("object_2");
		listOfValidStrings.add("object_3");
	}
	
	@Test
	public 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 JUnit assertThat

Download
You can download the full source code of this example here : junit-assertthat-example

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button