簡體   English   中英

Mockito,JUnit,Hamcrest,Versioning

[英]Mockito, JUnit, Hamcrest, Versioning

默認情況下,Hamcrest所需的版本為:

  • JUnit 4.11
    • Hamcrest 1.3
  • Mockito核心1.9.5
    • Hamcrest 1.1

Hamcrest 1.1和1.3之間沒有重要的API變化。 目前我的測試用例試圖用Hamcrest 1.1運行JUnit 4.11,但我有理由相信這是一個壞主意。 出於類似的原因,我懷疑嘗試將Mockito-core 1.9.5與Hamcrest 1.3一起使用也是一個壞主意。

該怎么辦?

  1. 將Hamcrest 1.1與最新的JUnit和Mockito一起使用
  2. 將Hamcrest 1.3與最新的JUnit和Mockito一起使用
  3. 嘗試修補Mockito-core 1.9.5以使用Hamcrest 1.3
    • 我此刻沒有的時間
  4. 使用JUnit 4.10
  5. 其他?

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

由@ durron597於2015年7月29日更新:這個出色的答案在2013年是正確的,但是由於Mockito的更新已經過時了。 看到這個答案。

我在許多Maven項目中使用最新的JUnit和Mockito核心依賴項以及hamcrest 1.3。 直到現在還沒有人報告這有任何問題。 因此,如果這適用於您的測試,請使用最新版本的所有三個版本。 只需確保使用mockito核心而不是全部。

所以我建議選擇2以獲得更新版本的所有好處。 如果你真的懷疑任何事情都可能出錯,請使用最安全的選項4。 但是當然你可以使用選項2,在不久的將來,任何事情都會出錯,你可以切換到選項2.或者從那以后,一個新的模擬器已經在那里解決了這個問題。

來自mockito的注釋問題397 :mockito-core沒有出現此問題。

這是mszalbach建議的Maven解決方案:

<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>

更新:截至2015年6月30日,Mockito的最新版本在內部使用Hamcrest 1.3。

因此,對於能夠升級到Mockito 2.0的用戶來說,這個問題已經過時了。

我不打算改變接受的答案,因為mszalbach應該保留15個代表,但這應該是新的規范答案

看看這里的Mockito文檔我認為選項2是推薦的方式(使用mockito-core工件)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM