What Is assertThat() Method?

What is assertThat() method? How can it be useful?

I had seen this method in mapreduce program in hadoop. Can anyone explain brief about it?

2 Answers

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.

You canfind some example on

The assertThat() belongs to the Matchers class of Hamcrest. This method does similar functionality as that of assertEquals() of the Assert class, but assertThat() is more human readable.

For example:

assertEquals(BigDecimal.valueOf(1.8).setScale(2, RoundingMode.HALF_UP), actual);
assertThat(actual, is(equalTo(BigDecimal.valueOf(1.8).setScale(2, RoundingMode.HALF_UP))));

You can see that assertThat() is more of an English sentence and easy to understand. The above sentence asserts that if the actual value is equal to the expected value given.

You can find some examples on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest