简体   繁体   中英

Mockito, JUnit, Hamcrest, Versioning

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

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.

This is the Maven solution suggested by mszalbach :

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.3</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <exclusions>
        <exclusion>
          <artifactId>hamcrest-core</artifactId>
          <groupId>org.hamcrest</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

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

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

看看这里的Mockito文档我认为选项2是推荐的方式(使用mockito-core工件)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM