日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

maven mockito_如何:测试Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中的依赖项

發(fā)布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 maven mockito_如何:测试Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中的依赖项 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

maven mockito

對于當今的大多數(shù)Java項目而言,JUnit本身還遠遠不夠。 您還需要一個模擬庫,也許還有其他東西。 在此迷你操作指南中,我介紹了可以在新的Java項目中開始的測試依賴項。

一切都始于JUnit

Maven存儲庫中的junit組中有兩個工件: junit和junit-dep 。 在4.9版之前,后者不包含對內(nèi)聯(lián)的Hamcrest的依賴。 今天,我們使用junit依賴關(guān)系如下:

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope> </dependency>

dependency:tree產(chǎn)生:

[INFO] \- junit:junit:jar:4.11:test [INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test

莫基托

我們通常需要的下一個依賴是一個模擬框架。 毫無疑問, Mockito是最受歡迎的游戲之一。 它有兩個好處: mockito-all和mockito-core 。 第一個是將所有依賴項內(nèi)聯(lián)到其中的單個jar,而后者只是Mockito。 建議將mockito-core與JUnit版本4.11一起使用。 因此,我們添加依賴項:

<dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>1.9.5</version><scope>test</scope> </dependency>

現(xiàn)在, dependency:tree產(chǎn)生:

[INFO] +- junit:junit:jar:4.11:test [INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] \- org.mockito:mockito-core:jar:1.9.5:test [INFO] \- org.objenesis:objenesis:jar:1.0:test

Hamcrest

知道m(xù)ockito-core更適合于聲明式依賴性管理,因此,我們將覆蓋對Hamcrest和Objenesis的依賴性,如下所示:

<dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId><version>1.3</version><scope>test</scope> </dependency><dependency><groupId>org.objenesis</groupId><artifactId>objenesis</artifactId><version>1.3</version><scope>test</scope> </dependency>

有了這個,我們可以輕松地添加Hamcrest庫,該庫提供了一個匹配對象庫,依賴項:

<dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId><version>1.3</version><scope>test</scope> </dependency><dependency><groupId>org.objenesis</groupId><artifactId>objenesis</artifactId><version>1.3</version><scope>test</scope> </dependency>

并且dependency:tree產(chǎn)生:

[INFO] +- junit:junit:jar:4.11:test [INFO] +- org.mockito:mockito-core:jar:1.9.5:test [INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] \- org.objenesis:objenesis:jar:1.3:test

斷言

AssertJ – Java的流暢斷言–提供了一組豐富而直觀的強類型斷言,可用于單元測試。 AssertJ是FEST Assert的一個分支,我前一段時間在這篇文章中寫過。 那依賴性呢? 讓我們來看看:

<dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>1.5.0</version><scope>test</scope> </dependency>

結(jié)果如下樹:

[INFO] +- junit:junit:jar:4.11:test [INFO] +- org.mockito:mockito-core:jar:1.9.5:test [INFO] +- org.assertj:assertj-core:jar:1.5.0:test [INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] \- org.objenesis:objenesis:jar:1.3:test

最終剪輯

完整的Maven結(jié)構(gòu)如下所示:

<!-- Test --> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope> </dependency> <dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>1.9.5</version><scope>test</scope> </dependency> <dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>1.5.0</version><scope>test</scope> </dependency> <dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId><version>1.3</version><scope>test</scope> </dependency> <dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-library</artifactId><version>1.3</version><scope>test</scope> </dependency> <dependency><groupId>org.objenesis</groupId><artifactId>objenesis</artifactId><version>1.3</version><scope>test</scope> </dependency>
  • 您可以在GitHub上的unit-testing-demo項目中找到它(鏈接到pom.xml ),也可以嘗試我的spring-mvc-quickstart-archetype (鏈接到pom.xml )。

參考:操作方法:在Codeleak.pl博客上,從我們的JCG合作伙伴 Rafal Borowiec中測試Maven項目(JUnit,Mockito,Hamcrest,AssertJ)中的依賴項 。

翻譯自: https://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.html

maven mockito

總結(jié)

以上是生活随笔為你收集整理的maven mockito_如何:测试Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中的依赖项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。