Spock 框架是 Java 和Groovy应用程序的测试和规范框架。Gradle是一种流行的构建工具,也是Maven 的替代品。
在本教程中,我们将展示如何使用 Gradle 设置项目并添加 Spock 测试依赖项。我们还将快速研究并逐步将 Spock 与 Spring 完全集成,仍然使用 Gradle 构建过程。
我们需要创建一个 Gradle 项目并添加 Spock 依赖项。
设置 Gradle 项目
首先,让我们在系统上安装 Gradle 。然后可以使用gradle init命令初始化Gradle 项目。例如,使用Java或 Kotlin 创建应用程序或库有不同的选项。
无论如何,Gradle 项目始终会从以下位置获取配置:
- build.gradle:它包含有关构建过程的信息,例如 Java 版本或用于实现或测试的库。我们将其称为构建文件。
- settings.gradle.:它添加项目信息,例如项目名称或子模块结构。我们将其称为设置文件。
Gradle 使用 JVM 插件来实现项目的编译、测试和捆绑功能。
如果我们选择 Java,我们将通过使用“ java ”插件来保持简单,因为这是它最终将扩展的内容。
让我们看一下 Java 17 项目的简单构建框架:
plugins { id 'java' } repositories { mavenCentral() } dependencies { // test dependencies testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } test { useJUnitPlatform() testLogging { events "started", "passed", "skipped", "failed" } }
|
该文件是核心组件,定义了构建项目所需的任务。
我们添加了JUnit5测试依赖项:
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
我们需要在测试任务中使用useJUnitPlatform()规范来运行测试。我们还添加了一些用于测试日志记录的属性,以便我们在任务运行时获得输出:
test { useJUnitPlatform() testLogging { events "started", "passed", "skipped", "failed" } }
|
我们还看到我们的项目如何使用mavenCentral()存储库下载依赖项:
repositories { mavenCentral() }
|
值得注意的是,有些人可能会发现该配置比具有基于 XML 的pom.xml构建配置的 Maven 项目更具可读性。
最后,我们还看一下设置文件:
rootProject.name = 'spring-boot-testing-spock'
这非常简单,只配置项目名称。但是,它可以包含相关信息,例如子模块包含或插件定义。
我们可以检查Gradle DSL 参考以获取有关构建或设置脚本的更多信息。
添加 Spock 依赖项
我们需要两个简单的步骤将 Spock 添加到 Gradle 项目中:
- 添加“groovy”插件
- 将 Spock 添加到测试依赖项
我们来看看构建文件:
plugins { id 'java' id 'groovy' } repositories { mavenCentral() } dependencies { // Spock test dependencies testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0' // Junit dependencies testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } test { useJUnitPlatform() testLogging { events "started", "passed", "skipped", "failed" } }
|
我们必须通过添加org.spockframework:spock-core 测试依赖项来更新我们的依赖项:
testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0'
值得注意的是,我们不需要像 Maven 项目那样配置GMavenPlus插件。
运行测试
我们可以使用 Spock 进行不同类型的测试。让我们看一个简单的测试用例:
class SpockTest extends Specification { def "one plus one should equal two"() { expect: 1 + 1 == 2 } }
|
每个测试都必须扩展规范类。此外,测试被定义为具有 Groovy def语法的函数。
如果我们习惯用 Java 编程,我们需要记住 Spock 测试默认位于不同的包中,并且有另一个类扩展。如果没有另外指定,我们必须将测试放在test/groovy文件夹中。此外,该类将具有.groovy扩展名,例如SpockTest.groovy。
要运行测试,我们需要使用 IDE 或在命令行执行测试任务:
gradle test
让我们检查一些示例输出:
Starting a Gradle Daemon (subsequent builds will be faster) > Task :test SpockTest > one plus one should equal two STARTED SpockTest > one plus one should equal two PASSED BUILD SUCCESSFUL in 15s 7 actionable tasks: 7 executed
|
Gradle 使用缓存系统,并且只会重新运行自上次执行以来发生更改的测试。
使用 Spock、Gradle 和 Spring
我们可能想将 Spock 添加到Spring项目中。Spock 有一个专门用于此目的的模块。
让我们看一下具有基本 Spring 配置的 Spock。稍后,我们还将了解 Spring Boot 设置。从现在开始,为了简洁起见,我们将省略构建文件中的java和测试部分,因为它们不会改变。
Spock 和Spring
假设我们有一个 Spring 项目,想要切换或采用 Spock 进行测试。
现在构建文件中的依赖结构变得更加复杂,因此让我们正确注释掉每个部分:
plugins { id 'java' id 'groovy' } repositories { mavenCentral() } dependencies { // Spring implementation dependencies implementation 'org.springframework:spring-web:6.1.0' // Spring test dependencies testImplementation 'org.springframework:spring-test:6.1.0' // Spring Spock test dependencies testImplementation 'org.spockframework:spock-spring:2.4-M1-groovy-4.0' // Spock Core test dependencies testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0' // Junit Test dependencies testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' }
|
我们添加org.springframework:spring-web来演示简单的 Spring 依赖关系。此外,如果我们想使用 Spring 测试功能,我们必须添加org.springframework:spring-test测试依赖:
// Spring implementation dependencies implementation 'org.springframework:spring-web:6.1.0' // Spring test dependencies testImplementation 'org.springframework:spring-test:6.1.0'
|
最后,让我们添加org.spockframework:spock-spring依赖项。这是我们集成 Spock 和 Spring 所需的唯一依赖项:
// Spring Spock test dependencies testImplementation 'org.spockframework:spock-spring:2.4-M1-groovy-4.0'
|
Spock 和 Spring Boot
我们只需要将之前Spring的基础依赖替换为Spring Boot的即可。
我们来看看构建文件:
plugins { id 'java' id 'groovy' } repositories { mavenCentral() } dependencies { // Spring implementation dependencies implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0' // Spring Test dependencies testImplementation 'org.springframework.boot:spring-boot-starter-test:3.0.0' // Spring Spock Test dependencies testImplementation 'org.spockframework:spock-spring:2.4-M1-groovy-4.0' // Spring Core Test dependencies testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0' // Junit Test dependencies testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' }
|
值得注意的是,我们添加了 Spring Boot 依赖项:
// Spring implementation dependencies implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0' // Spring Test dependencies testImplementation 'org.springframework.boot:spring-boot-starter-test:3.0.0'
|
Spring 和 Gradle 依赖管理
让我们使用Spring 依赖管理来完成我们的设置,以使我们的配置更加紧凑和可维护。这样,我们采用类似于Maven项目的 BOM 或“物料清单”导向风格,并且仅在单个位置声明版本。
我们可以通过几种方法来实现这一目标。
我们来看看第一个选项:
plugins { id 'java' id 'groovy' id "org.springframework.boot" version "3.0.0" id 'io.spring.dependency-management' version '1.0.14.RELEASE' } repositories { mavenCentral() } dependencies { // Spring implementation dependencies implementation 'org.springframework.boot:spring-boot-starter-web' // Spring Test dependencies testImplementation 'org.springframework.boot:spring-boot-starter-test' // Spring Spock Test dependencies testImplementation 'org.spockframework:spock-spring:2.4-M1-groovy-4.0' // Spring Core Test dependencies testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0' }
|
在这种情况下,我们只需添加以下插件:
id "org.springframework.boot" version "3.2.1"
id 'io.spring.dependency-management' version '1.0.14.RELEASE'
值得注意的是,JUnit5 依赖项现在由 Spring Boot 拉取,无需指定它们。
最后,如果我们想要更新Spring Boot版本,只需要在org.springframework.boot插件中进行替换即可。
我们来看看第二个选项:
plugins { id 'java' id 'groovy' id 'io.spring.dependency-management' version '1.1.4' } repositories { mavenCentral() } dependencyManagement { imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:3.2.1' } } dependencies { // Spring implementation dependencies implementation 'org.springframework.boot:spring-boot-starter-web' // Test implementation testImplementation( 'junit:junit', 'org.spockframework:spock-core:2.4-M1-groovy-4.0', 'org.spockframework:spock-spring:2.4-M1-groovy-4.0', 'org.springframework.boot:spring-boot-starter-test', ) }
|
现在,我们已将org.springframework.boot插件替换为dependentecyManagent部分,作为依赖项的单个入口点:
dependencyManagement { imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:3.2.1' } }
|
值得注意的是,我们将testImplementation折叠到一个输入中,并添加 JUnit4 依赖项,以防我们的项目仍然使用它或与 JUnit5 混合:
// Test implementation testImplementation( 'junit:junit', 'org.spockframework:spock-core:2.4-M1-groovy-4.0', 'org.spockframework:spock-spring:2.4-M1-groovy-4.0', 'org.springframework.boot:spring-boot-starter-test', )
|
结论
在本教程中,我们了解了如何使用 Spock 和 Gradle 建立 Java 项目。我们还了解了如何添加 Spring 和 Spring Boot 依赖项。Gradle 在我们的项目脚本设置中提供了强大的构建支持和更少的冗长。同样,Spock 是一个很棒的测试工具,因为它易于设置和规范,面向数据驱动测试和基于交互,而不是 JUnit 的类似断言形式。