10

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?

3 Answers 3

4

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 https://www.kloia.com/blog/better-unit-testing-with-hamcrest

Sign up to request clarification or add additional context in comments.

Comments

3

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 https://examples.javacodegeeks.com/core-java/junit/junit-assertthat-example/

1 Comment

Assert.assertThat() has been deprecated a while ago. It is now recommended to use the MatcherAssert.assertThat() method
1

The assertThat() method commonly used with tests of AssertJ module – an opensource community-driven library used for writing fluent and rich assertions in Java tests.

I would suggest you to read here https://www.baeldung.com/introduction-to-assertj

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.