用于混合Spock 1.x和JUnit 5测试的Maven项目设置
我為Maven創建了一個示例Groovy項目,該項目在一個項目中混合了Spock測試和JUnit 5測試。 在下一節中,我將描述如何設置這種Maven項目。
在項目中啟用Groovy
首先,您必須在項目中啟用Groovy。 一種可能性是將GMavenPlus插件添加到您的項目中。
< build > < plugins > < plugin > < groupId >org.codehaus.gmavenplus</ groupId > < artifactId >gmavenplus-plugin</ artifactId > < version >1.6.2</ version > < executions > < execution > < goals > < goal >addSources</ goal > < goal >addTestSources</ goal > < goal >compile</ goal > < goal >compileTests</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build >目標addSources和addTestSources將Groovy(測試)源添加到Maven的主要(測試)源。 默認位置是src / main / groovy (對于主源)和src / test / groovy (對于測試源)。 目標編譯和compileTests編譯Groovy(測試)代碼。 如果您沒有Groovy主代碼,則可以省略addSource和compile 。
上面的配置始終使用最新發布的Groovy版本。 如果要確保使用特定的Groovy版本,則必須將特定的Groovy依賴項添加到類路徑中。
< dependencies > < dependency > < groupId >org.codehaus.groovy</ groupId > < artifactId >groovy</ artifactId > < version >2.5.6</ version > </ dependency > </ dependencies >在項目中啟用JUnit 5
在項目中使用JUnit 5的最簡單設置是在測試類路徑中添加JUnit Jupiter依賴關系,并配置正確版本的Maven Surefire插件(至少為2.22.0版)。
< dependencies > <!--... maybe more dependencies --> < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < dependencyManagement > < dependencies > < dependency > < groupId >org.junit</ groupId > < artifactId >junit-bom</ artifactId > < version >${junit.jupiter.version}</ version > < scope >import</ scope > < type >pom</ type > </ dependency > </ dependencies > </ dependencyManagement > < build > < plugins > <!-- other plugins --> < plugin > < artifactId >maven-surefire-plugin</ artifactId > < version >2.22.1</ version > </ plugin > </ plugins > </ build >在項目中啟用Spock
選擇正確的Spock依賴項取決于您在項目中使用的Groovy版本。 在我們的例子中,是Groovy 2.5版。 因此,我們在測試類路徑中需要版本1.x-groovy-2.5的Spock。
< dependencies > <!-- more dependencies --> < dependency > < groupId >org.spockframework</ groupId > < artifactId >spock-core</ artifactId > < version >1.3-groovy-2.5</ version > < scope >test</ scope > </ dependency > </ dependencies >現在期望Spock測試和JUnit5測試在Maven構建中執行。 但是Maven只執行JUnit5測試。 所以發生了什么事?
我開始將Maven Surefire插件版本更改為2.21.0。 然后執行了Spock測試,但沒有執行JUnit5測試。 原因是,在Maven Surefire插件的2.22.0版本中,默認情況下,JUnit Platform Provider替換了JUnit4 provider。 但是版本1.x中的Spock基于JUnit4。 這將在Spock版本2中進行更改。此版本將基于JUnit5平臺。 因此,對于Spock 1.x,我們必須將JUnit Vintage依賴項添加到測試類路徑中。
< dependencies > <!-- more dependencies --> < dependency > <!--Only necessary for surefire to run spock tests during the maven build --> < groupId >org.junit.vintage</ groupId > < artifactId >junit-vintage-engine</ artifactId > < scope >test</ scope > </ dependency > </ dependencies >這允許在JUnit平臺上運行較早的JUnit(3/4)測試。 使用此配置,Spock和JUnit 5測試都在Maven構建中執行。
鏈接
- Groovy的示例Maven設置,包括Github上的JUnit 5和Spock
- Maven GMaven Plus插件
- Maven Surefire插件–使用JUnit 5平臺
- JUnit 5用戶指南
- Spock框架
翻譯自: https://www.javacodegeeks.com/2019/03/maven-project-setup-mixing-spock-junit-5-tests.html
總結
以上是生活随笔為你收集整理的用于混合Spock 1.x和JUnit 5测试的Maven项目设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 尼康 Z f 全幅复古相机新品外观曝光,
- 下一篇: 您是否尝试过MicroProfile S