使用IntelliJ IDEA中的Groovy Spock单元测试设置一个新的Sping Boot Java项目。我无法运行我的第一个单元测试。我从IntelliJ内部创建了它,它位于src/test/groovy下。
下面是单元测试。
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()
}
我试图找到与此相关的所有问题,但似乎没有一个有帮助。它似乎没有找到我的单元测试源代码。
3条答案
按热度按时间c6ubokkw1#
您需要将Groovy插件添加到构建文件中:
这也将帮助IDEA认识到这是一个Groovy项目,您可以从
src/test/groovy
运行测试。接下来,您将得到测试编译错误,因为您使用的
org.spockframework:spock-bom:2.0-M1-groovy-2.5
与org.codehaus.groovy:groovy-all:3.0.5
,即
所以要么你降级Groovy,要么升级Spock。另外,M1不是最新的,最好使用M3。
现在您的测试应该编译并运行了。不过我没有像这样检查测试。没有测试中的类,我无论如何都不能运行它。
摆脱
org.spockframework:spock-junit4
依赖项是可选的,正如前面所说,Spock 2.x基于JUnit 5,因此如果您的项目中没有任何使用JUnit 4特性的遗留Spock测试,如PowerMock的@RunWith
或类似的丑陋的东西,那么您就不需要它。也许您希望将构建文件更改为更类似于Spock sample project中的构建文件。
sourceCompatibility = '11'
之类的内容,但在8之类的旧JDK上运行构建时需要小心。这将导致以下违反直觉的Gradle错误:仅当使用Gradle参数
--debug
运行测试时,您才会看到根本原因(日志标签和时间戳已删除):7xzttuei2#
对我来说,这个解决方案。只是删除:
这里是我的建设。gradle:
ckocjqey3#
添加以下内容对我来说就像上面提到的Alex一样有效。