Difference between hamcrest-library Matchers and hamcrest-core CoreMatchers

后端 未结 3 755
天涯浪人
天涯浪人 2021-02-04 22:55

It looks like the hamcrest org.hamcrest.Matchers class is very similar to org.hamcrest.CoreMatchers (though it looks like Matchers has mor

3条回答
  •  时光取名叫无心
    2021-02-04 23:34

    The Hamcrest matchers are split into several modules. The "core" includes the most basic matchers and abstract classes required for building other matchers. org.hamcrest.CoreMatchers includes the factory methods for just these matchers. The other matchers are in the "library" module grouped by the types of objects they match and are optional. org.hamcrest.Matchers includes both sets of matchers.

    Which should you use? I statically import everything from the latter without any trouble. Perhaps the compile times might take slightly longer, but that's never been an issue for me. I put this at the top of my unit tests in addition to the JUnit imports:

    import static org.hamcrest.MatcherAssert.*;
    import static org.hamcrest.Matchers.*;
    

    This gives the best readability in the test methods.

提交回复
热议问题