Hamcrest - 使用什么版本? 1.3 或 2

Hamcrest - what version to use? 1.3 or 2

我很困惑。目前我正在使用

测试我的 spring 应用程序
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

只要我想匹配RegularExpressions,我就很高兴。在 hamcrest 1.3 中,您需要编写自己的匹配器,我不太喜欢它。 我搜索并发现 hamcrest 2.0 内置了一些东西,比如:

assertThat(DateHelper.getActualDateForXML(), MatchesPattern.matchesPattern("\d{4}+-\d{2}-+\d{2}+T\d{2}+:\d{2}+:\d{2}+"));

我很高兴,我补充说:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-junit</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>

并从 spring-boot-starter-test:

中踢出 1.3 hamcrest 依赖项
<exclusions>
    <exclusion>
       <groupId>org.hamcrest</groupId>
       <artifactId>hamcrest-library</artifactId>
    </exclusion>
    <exclusion>
       <groupId>org.hamcrest</groupId>
       <artifactId>hamcrest-core</artifactId>
    </exclusion>
</exclusions>

现在一切都像我期望的那样正常工作,但我感觉不太舒服。因为我只能找到写1.3的人而不能真正找到hamcrest-junit 2.0的使用。

谁能解释一下它们之间的联系?因为 hamcrest-junit 2.0 似乎拥有从 hamcrest 1.3 构建的所有内容...

谢谢本

编辑:几年后 答案是使用最新的 Hamcrest 2 版本 (2.2 from 17th October 2019). For additional details also refer to .

以下是我的原始答案,我将其作为上下文来理解当时围绕 Hamcrest 1.3 和 2.0.0.0 版本的问题和困惑。


基于 Hamcrest Github

JUnit Lambda (Junit 5)

我的看法

  • hamcrest-junit 2.0(应该是)是一个新的开始,其目标也是将 hamcrest 与 junit 分离
  • 同时 Junit 5 项目启动,reduces/removes 第三方依赖项

=> 在这种情况下,我希望 hamcrest 的人在实质性推进之前等待/协调 JUnit 5 项目是明智的。

正如@heenenee 所提到的,hamcrest 的家伙开始忙于其他事情,所以此时项目没有太多进展。

回答

Can somebody explain me the connection between them? Because it seems that hamcrest-junit 2.0 has everything build in from hamcrest 1.3

  • hamcrest-junit 2.0 开始了,但它背后的人很快(现在仍然)忙于其他项目,所以开发停止了
  • 关于 JUnit 5 也可能存在一些不确定性,这可能会促使进一步推迟 hamcrest-junit 2.0 开发,直到 JUnit 5 发布(推测)

... but I do not feel really comfortable. Because I can just find people writing about 1.3 and can not really find the use of hamcrest-junit 2.0.

目前,除了你的情况外,没有太多动力转向 hamcrest-junit 2.0。一旦 Junit 5 发布,我预计会有更多的动力再次向前发展。

Ivo, which really should be getting more votes, but here's some added information. If you check Maven for information on the dependencies, Java Hamcrest 2.0.0.0 and Hamcrest JUnit 2.0.0.0, there are some usage 链接提供了很好的答案。写这篇的时候分别是74和68,浏览了一下没有什么大项目。

Hamcrest 项目刚刚 released 2.1, and they reworked the package structure after thorough consideration. Make sure you follow the upgrade procedure。 工件 java-hamcrest 2.0.0.0 现在被认为是 failed attempt