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?
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?
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
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/
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
© 2022 - 2025 — McMap. All rights reserved.
Assert.assertThat()
has been deprecated a while ago. It is now recommended to use theMatcherAssert.assertThat()
method – Sculpin