java – org. junit. Assert. Assert is better than org hamcrest. MatcherAssert. assertThat?

I am new to JUnit and hamcrest and want best practice advice, so I can decide which documents to study first

For beginners, which of these assertthat methods is better?

> org. junit. Assert. assertThat(from junit-4.11.jar) > org. hamcrest. MatcherAssert. Assert that (from hamcrest-core-1.3. Jar)

According to one last year, "JUnit has the assertthat method, but hamcrest has its own assertthat method that does the same thing."

Earlier this year, hamcrest "could potentially give better error messages because the matcher is called to describe the mismatch"

It's hard to say which versions of JUnit and hamcrest. Com these posts compare So I want a recommendation based on the latest version

Solution

This is almost the same thing

The latest version of JUnit now includes hamcrest

Infact org. junit. Assert. The assertt method signature is

public static <T> void assertThat(T actual,org.hamcrest.Matcher<T> matcher)

You'll notice using the hamcrest matcher

You may still want to include your own version of hamcrest, because JUnit is not updated frequently and does not always use the latest version of hamcrest

According to Maven POM, JUnit 4.11 uses hamcrest 1.3, which I believe is the latest in writing

Editor: I just read your second article http://blog.code-cop.org/2014/02/assert-or-matcherassert.html , it describes two subtle differences in hamcrest assert, which makes it more useful:

>When the match fails, the error message includes a different "expected x but y" The custom hamcrest matcher may include more detailed information. By implementing describemismatch() > assertthat signature, the matcher super t matcher allows the matcher to be a super type (such as matcher to compare integer and double precision) This usually doesn't matter, but it's a good feature when you need it

So use org hamcrest. MatcherAssert. assertThat

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>