Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito, JUnit, Hamcrest, Versioning

Tags:

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

like image 226
durron597 Avatar asked Sep 12 '13 17:09

durron597


People also ask

Does Mockito use Hamcrest?

Requires hamcrest on classpath, Mockito does not depend on hamcrest!

Is matcher Hamcrest?

Introduction. Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.

Does JUnit 5 work with Mockito?

Mockito provides an implementation for JUnit5 extensions in the library – mockito-junit-jupiter.

What is the use of @ExtendWith MockitoExtension class?

The @ExtendWith annotation is used to load a JUnit 5 extension. JUnit defines an extension API, which allows a third-party vendor like Mockito to hook into the lifecycle of running test classes and add additional functionality.


2 Answers

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.

like image 148
mszalbach Avatar answered Sep 20 '22 11:09

mszalbach


Update: As of June 30, 2015, the latest version of Mockito is using Hamcrest 1.3 internally.

  • Github commit
  • Maven central link

Therefore, this issue is obsolete for those able to upgrade to Mockito 2.0.

I am not going to change the accepted answer because mszalbach deserves to keep the 15 rep, but this should be the new canonical answer

like image 41
durron597 Avatar answered Sep 20 '22 11:09

durron597