Mockito, JUnit, Hamcrest, Versioning

durron597 picture durron597 · Sep 12, 2013 · Viewed 7.6k times · Source

By default, the required version of Hamcrest for:

  • JUnit 4.11
    • Hamcrest 1.3
  • Mockito-core 1.9.5
    • Hamcrest 1.1

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.

What to do?

  1. Use Hamcrest 1.1 with the latest JUnit and Mockito
  2. Use Hamcrest 1.3 with the latest JUnit and Mockito
  3. Attempt to patch Mockito-core 1.9.5 to use Hamcrest 1.3
    • Time I don't really have at the moment
  4. Use JUnit 4.10
  5. Other?

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

Answer

mszalbach picture mszalbach · Sep 12, 2013

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.

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.

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