Gradle 未找到spring boot应用程序的Spock单元测试中包含的给定测试
使用IntelliJ IDEA中的groovy Spock单元测试设置新的spring boot java项目。我无法运行我的第一个单元测试。我从IntelliJ内部创建了它,它位于src/test/groovy下 下面是单元测试Gradle 未找到spring boot应用程序的Spock单元测试中包含的给定测试,gradle,intellij-idea,groovy,spock,Gradle,Intellij Idea,Groovy,Spock,使用IntelliJ IDEA中的groovy Spock单元测试设置新的spring boot java项目。我无法运行我的第一个单元测试。我从IntelliJ内部创建了它,它位于src/test/groovy下 下面是单元测试 package com.heavyweightsoftware.orders.pojo import com.heavyweightsoftware.util.FixedDecimal import spock.lang.Specification class I
package com.heavyweightsoftware.orders.pojo
import com.heavyweightsoftware.util.FixedDecimal
import spock.lang.Specification
class ItemOrderedTest extends Specification {
public static final String ITEM_ID = "OU812"
public static final String ITEM_NAME = "Eddie V."
public static final int ITEM_QUANTITY = 5
public static final FixedDecimal ITEM_UNIT_PRICE = new FixedDecimal(35.62, 2)
static ItemOrdered getItemOrdered() {
ItemOrdered result = new ItemOrdered()
result.itemId = ITEM_ID
result.quantity = ITEM_QUANTITY
result.itemName = ITEM_NAME
result.unitPrice = ITEM_UNIT_PRICE
return result;
}
def "GetQuantity"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
int result = testItem.quantity
then: "Should be correct"
result == ITEM_QUANTITY
}
def "GetItemId"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
String result = testItem.itemId
then: "Should be correct"
result == ITEM_ID
}
def "GetItemName"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
String result = testItem.itemName
then: "Should be correct"
result == ITEM_NAME
}
def "GetLineTotal"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Total"
FixedDecimal result = testItem.lineTotal
then: "Should be correct"
FixedDecimal total = new FixedDecimal(178.10, 2)
result == total
}
def "GetUnitPrice"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
FixedDecimal result = testItem.unitPrice
then: "Should be correct"
result == ITEM_UNIT_PRICE
}
}
这是当我点击IntelliJ中的双箭头运行完整单元测试时的输出
Testing started at 12:47 PM ...
Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:33391', transport: 'socket'
Gradle Daemon started in 621 ms
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [com.heavyweightsoftware.orders.pojo.ItemOrderedTest](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 4s
4 actionable tasks: 1 executed, 3 up-to-date
Disconnected from the target VM, address: '127.0.0.1:33391', transport: 'socket'
以及测试设置的屏幕截图:
这是我的build.gradle文件:
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.heavyweightsoftware'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
// for heavyweight software dependencies
flatDir {
dirs 'libs'
}
// Spock snapshots are available from the Sonatype OSS snapshot repository
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
// heavyweight software libraries
compile fileTree(dir: 'libs', include: ['*.jar'])
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'
// mandatory dependencies for using Spock
compile "org.codehaus.groovy:groovy-all:3.0.5"
testImplementation platform("org.spockframework:spock-bom:2.0-M1-groovy-2.5")
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-junit4" // you can remove this if your code does not rely on old JUnit 4 rules
// optional dependencies for using Spock
testImplementation "org.hamcrest:hamcrest-core:1.3" // only necessary if Hamcrest matchers are used
testImplementation "net.bytebuddy:byte-buddy:1.9.3" // allows mocking of classes (in addition to interfaces)
testImplementation "org.objenesis:objenesis:2.6" // allows mocking of classes without default constructor (together with CGLIB)
// spring dependencies
implementation (
'org.springframework.boot:spring-boot-configuration-processor',
'org.springframework.boot:spring-boot-starter-data-jpa',
'org.springframework.boot:spring-boot-starter-web',
)
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
我试图找到所有与此相关的问题,但似乎没有一个能起到帮助作用。它似乎找不到我的单元测试源代码。免责声明:我是100%的Maven用户。Gradle对我来说更像是试错。但我对斯波克的问题感兴趣,所以我想尝试一下
package com.heavyweightsoftware.orders.pojo
import com.heavyweightsoftware.util.FixedDecimal
import spock.lang.Specification
class ItemOrderedTest extends Specification {
public static final String ITEM_ID = "OU812"
public static final String ITEM_NAME = "Eddie V."
public static final int ITEM_QUANTITY = 5
public static final FixedDecimal ITEM_UNIT_PRICE = new FixedDecimal(35.62, 2)
static ItemOrdered getItemOrdered() {
ItemOrdered result = new ItemOrdered()
result.itemId = ITEM_ID
result.quantity = ITEM_QUANTITY
result.itemName = ITEM_NAME
result.unitPrice = ITEM_UNIT_PRICE
return result;
}
def "GetQuantity"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
int result = testItem.quantity
then: "Should be correct"
result == ITEM_QUANTITY
}
def "GetItemId"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
String result = testItem.itemId
then: "Should be correct"
result == ITEM_ID
}
def "GetItemName"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
String result = testItem.itemName
then: "Should be correct"
result == ITEM_NAME
}
def "GetLineTotal"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Total"
FixedDecimal result = testItem.lineTotal
then: "Should be correct"
FixedDecimal total = new FixedDecimal(178.10, 2)
result == total
}
def "GetUnitPrice"() {
given: "A test item"
ItemOrdered testItem = getItemOrdered()
when: "Getting Value"
FixedDecimal result = testItem.unitPrice
then: "Should be correct"
result == ITEM_UNIT_PRICE
}
}
您需要将Groovy插件添加到构建文件中:
插件{
// ...
id‘java’
id‘groovy’
}
这也将有助于IDEA认识到这是一个Groovy项目,您可以从src/test/Groovy
运行测试
接下来,您将得到测试编译错误,因为使用
vsorg.spockframework:spockbom:2.0-M1-groovy-2.5
org.codehaus.groovy:groovy-all:3.0.5
- Groovy 2.5的Spock版本
- 但是Groovy 3
//使用Spock的强制依赖项
testCompile“org.codehaus.groovy:groovy:2.5.13”
测试实现平台(“org.spockframework:spockbom:2.0-M3-groovy-2.5”)
现在您的测试应该编译并运行。不过,我没有检查测试本身。如果没有测试中的类,我无论如何都无法运行它
摆脱org.spockframework:spock-junit4
依赖关系是可选的。如前所述,Spock 2.x基于JUnit 5,因此,如果您的项目中没有任何使用JUnit 4功能(如PowerMock的@RunWith
)或类似丑陋东西的遗留Spock测试,您就不需要它
也许您想将构建文件更改为与中的更相似。为我设计了此解决方案。只需删除:
test {
useJUnitPlatform()
}
这是我的身材。格雷德尔:
plugins {
id 'java'
id 'groovy'
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.yourcompany.common.dataflow.plugin' version '0.1.13'
id 'com.github.jk1.dependency-license-report' version '1.16'
}
group = 'org.yourcompany.dataflow'
version = '0.0.1'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
maven {
url "http://nexus.yourcompany.ru:8081/repository/maven-public/"
}
}
ext {
set('springCloudVersion', "2020.0.1")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-stream'
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.kafka:spring-kafka'
// third party
compile 'io.dgraph:dgraph4j:20.11.0'
// yourcompany
implementation 'org.yourcompany:common_dataflow_annotations:0.1.11'
implementation('org.yourcompany:common_service:0.2.27') {
exclude group: 'io.springfox', module: 'springfox-swagger2'
}
// test and dev
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.5.6'
testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.3-groovy-2.5'
testImplementation group: 'org.spockframework', name: 'spock-spring', version: '1.3-groovy-2.5'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.kafka:spring-kafka-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
显示您的Gradle依赖项。特别是,Spock1生成JUnit4注释,Spock2生成JUnit5注释;您的测试运行程序可能与您的Spock版本不匹配。(请注意,如果您可以采用Groovy的便利功能,您的工作效率会更高。一个主要的功能是消除对“临时变量”的需要,如生成器方法:
返回新的ItemOrdered(itemId:ITEM\u ID,quantity:ITEM\u quantity,itemName:ITEM\u NAME,unitPrice:ITEM\u UNIT\u PRICE)
)@chrylis小心光学-我想你的意思是从我的build.gradle文件。我已经复制了这些值。删除spock-junit4
依赖项,看看它是否有效。请注意,您明确排除了“Vintage”引擎,它是可以执行JUnit 4测试的JUnit 5引擎(我相信这是从Spring Initializer默认生成的)。@chrylis谨慎地提出了一个好主意,但在更改和刷新之后,得到了相同的精确结果。这似乎已经做到了。我添加了groovy插件(很抱歉,我没有注意到,Spring为我构建了这个插件)"然后注意到我的src/test/groovy文件夹没有标记为测试源。我标记了它,然后运行了测试,它成功了。谢谢你的帮助。不,如果你想确保Gradle构建文件是正确的,你不能手动标记它!你可以手动或自动重新导入Gradle构建,看看IDEA是否自动识别源文件夹ally.Gradle应该始终是领先的系统。如果你必须进行额外的定制,你的想法构建将是不可靠的,以后可能会被Gradle重新导入覆盖。然后在下一个工作站上,你的同事开始想,为什么构建在你的机器上运行,而不是在她的机器上运行。所以不要这样做!伙计,你为我节省了一半的时间谢谢你。