spock测试_用于混合Spock 1.x和JUnit 5测试的Maven项目设置
spock測(cè)試
我為Maven創(chuàng)建了一個(gè)示例Groovy項(xiàng)目,該項(xiàng)目在一個(gè)項(xiàng)目中混合了Spock測(cè)試和JUnit 5測(cè)試。 在下一節(jié)中,我將描述如何設(shè)置這種Maven項(xiàng)目。
在項(xiàng)目中啟用Groovy
首先,您必須在項(xiàng)目中啟用Groovy。 一種可能性是將GMavenPlus插件添加到您的項(xiàng)目中。
< 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 >目標(biāo)addSources和addTestSources將Groovy(測(cè)試)源添加到Maven的主要(測(cè)試)源。 默認(rèn)位置是src / main / groovy (對(duì)于主源)和src / test / groovy (對(duì)于測(cè)試源)。 目標(biāo)編譯和compileTests編譯Groovy(測(cè)試)代碼。 如果您沒有Groovy主代碼,則可以省略addSource和compile 。
上面的配置始終使用最新發(fā)布的Groovy版本。 如果要確保使用特定的Groovy版本,則必須將特定的Groovy依賴項(xiàng)添加到類路徑中。
< dependencies > < dependency > < groupId >org.codehaus.groovy</ groupId > < artifactId >groovy</ artifactId > < version >2.5.6</ version > </ dependency > </ dependencies >在項(xiàng)目中啟用JUnit 5
在項(xiàng)目中使用JUnit 5的最簡(jiǎn)單設(shè)置是在測(cè)試類路徑中添加JUnit Jupiter依賴關(guān)系,并配置正確版本的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 >在項(xiàng)目中啟用Spock
選擇正確的Spock依賴項(xiàng)取決于您在項(xiàng)目中使用的Groovy版本。 在我們的例子中,是Groovy 2.5版。 因此,我們?cè)跍y(cè)試類路徑中需要版本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 >現(xiàn)在期望Spock測(cè)試和JUnit5測(cè)試在Maven構(gòu)建中執(zhí)行。 但是Maven只執(zhí)行JUnit5測(cè)試。 所以發(fā)生了什么事?
我開始將Maven Surefire插件版本更改為2.21.0。 然后執(zhí)行了Spock測(cè)試,但沒有執(zhí)行JUnit5測(cè)試。 原因是在Maven Surefire插件的2.22.0版本中,默認(rèn)情況下,JUnit Platform Provider替換了JUnit4 provider。 但是版本1.x中的Spock基于JUnit4。 這將在Spock版本2中進(jìn)行更改。此版本將基于JUnit5平臺(tái)。 因此,對(duì)于Spock 1.x,我們必須將JUnit Vintage依賴項(xiàng)添加到測(cè)試類路徑中。
< 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平臺(tái)上運(yùn)行較早的JUnit(3/4)測(cè)試。 使用此配置,Spock和JUnit 5測(cè)試都在Maven構(gòu)建中執(zhí)行。
鏈接
- Groovy的示例Maven設(shè)置,包括Github上的JUnit 5和Spock
- Maven GMaven Plus插件
- Maven Surefire插件–使用JUnit 5平臺(tái)
- JUnit 5用戶指南
- Spock框架
翻譯自: https://www.javacodegeeks.com/2019/03/maven-project-setup-mixing-spock-junit-5-tests.html
spock測(cè)試
總結(jié)
以上是生活随笔為你收集整理的spock测试_用于混合Spock 1.x和JUnit 5测试的Maven项目设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑dxdiag配置怎么看(怎么看电脑装
- 下一篇: java查找链表中间元素_如何通过Jav