简体   繁体   中英

The “is” in JUnit 4 assertions

Is there any semantic difference between writing

assertThat(object1, is(equalTo(object2)));

and writing

assertThat(object1, equalTo(object2)));

? If not, I would prefer the first version, because it reads better. Are there any other considerations here?

Documentation says it all:
Decorates another Matcher, retaining the behavior but allowing tests to be slightly more expressive.
eg. assertThat(cheese, equalTo(smelly))
vs assertThat(cheese, is(equalTo(smelly)))

http://www.junit.org/apidocs/org/hamcrest/core/Is.html
In other words, you're on the right track.

They are equivalent, as far as I'm aware. The " Is " matcher just passes through to the contained matcher. It seems it's there to add readability, and perhaps backwards compatibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM