Why should I use Hamcrest-Matcher and assertThat()

2019-01-29 17:45发布

When I look at the examples in the Assert class JavaDoc

assertThat("Help! Integers don't work", 0, is(1)); // fails:
// failure message:
// Help! Integers don't work
// expected: is <1> 
// got value: <0>
assertThat("Zero is one", 0, is(not(1))) // passes

I dont see a big advantage over, let's say, assertEquals( 0, 1 ).

It's nice maybe for the messages if the constructs get more complicated but do you see more advantages? Readability?

7条回答
Explosion°爆炸
2楼-- · 2019-01-29 18:36
assertThat(frodo.getName()).isEqualTo("Frodo");

Is close to natural language.

Easier read, easier analyze code. Programer spend more time to analyze code than write new one. So if code will be easy to analyze then developer should be more productive.

P.S. Code should be as well-written book. Self documented code.

查看更多
登录 后发表回答