What is assertThat() method?
Asked Answered
B

3

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?

Bernetta answered 27/8, 2016 at 8:33 Comment(0)
A
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

Annetteannex answered 4/2, 2022 at 8:41 Comment(0)
C
2

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/

Crinkumcrankum answered 27/8, 2016 at 8:36 Comment(1)
Assert.assertThat() has been deprecated a while ago. It is now recommended to use the MatcherAssert.assertThat() methodSculpin
S
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

Selry answered 25/3, 2023 at 12:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.