Mockito、JUnit、Hamcrest、版本控制

2022-01-14 00:00:00 dependencies mockito java junit hamcrest

默认情况下,Hamcrest 所需的版本用于:

By default, the required version of Hamcrest for:

  • JUnit 4.11
    • Hamcrest 1.3
    • Hamcrest 1.1

    Hamcrest 1.1 和 1.3 之间的 API 没有变化.目前我的测试用例尝试使用 Hamcrest 1.1 运行 JUnit 4.11,但我合理 确定这是一个坏主意.出于类似的原因,我怀疑尝试将 Mockito-core 1.9.5 与 Hamcrest 1.3 一起使用是 也是个坏主意.

    There were not insiginifcant API changes between Hamcrest 1.1 and 1.3. Currently my test cases attempt to run JUnit 4.11 with Hamcrest 1.1, but I'm reasonably sure that this is a bad idea. For similar reasons, I suspect that trying to use Mockito-core 1.9.5 with Hamcrest 1.3 is also a bad idea.

    怎么办?

    1. 将 Hamcrest 1.1 与最新的 JUnit 和 Mockito 结合使用
    2. 将 Hamcrest 1.3 与最新的 JUnit 和 Mockito 结合使用
    3. 尝试修补 Mockito-core 1.9.5 以使用 Hamcrest 1.3
      • 我现在真的没有时间

    2015-06-12 更新: Mockito 1.10.19 和 2.0.13-beta 仍然使用 Hamcrest 1.1

    UPDATE 2015-06-12: Both Mockito 1.10.19 and 2.0.13-beta still use Hamcrest 1.1

    推荐答案

    @durron597 于 2015 年 7 月 29 日更新:这个出色的答案在 2013 年是正确的,但后来因为 Mockito 的更新而过时.查看此答案.

    我在工作中的许多 Maven 项目中使用具有 Mockito 核心依赖项和 hamcrest 1.3 的最新 JUnit.到目前为止,没有人报告这有任何问题.因此,如果这适用于您的测试,请使用所有三个的最新版本.只需确保使用 mockito 核心而不是全部.

    Update July 29, 2015 by @durron597: This excellent answer was correct in 2013, however it has since been rendered obsolete by an update to Mockito. See this answer.

    I use latest JUnit with Mockito core dependency and hamcrest 1.3 in many Maven projects at work. Till now nobody reported any problem with this. So if this works for your tests go with the newest Version for all three. Just ensure to use mockito core instead of all.

    所以我建议使用选项 2 以获得新版本的所有好处.如果您真的怀疑任何事情都可能出错,请使用最安全的选项 4.但当然你可以选择选项 2,当在不久的将来出现任何问题时,你可以切换到选项 2.或者从那时起,一个更新的 mockito 已经可以解决这个问题.

    So I would recommend to go with option 2 to have all benefits of the newer versions. If you are really in doubt that anything could go wrong use option 4 which is the safest one. But of course you could go with option 2 and when in the near future anything would be wrong you could switch to option 2. Or since then a newer mockito is already out there solving this.

    来自 mockito 问题 397 的注释:此问题不与 mockito-core 一起出现.

    Note from mockito Issue 397: This problem does not appear with mockito-core.

相关文章