透過 Gradle 設定和使用 Spock
1. 概述
Spock 框架是 Java 和 Groovy 應用程式的測試和規格框架。 Gradle是一種流行的建置工具,也是 Maven 的替代品。
在本教程中,我們將展示如何使用 Gradle 設定專案並新增 Spock 測試依賴項。我們還將快速研究並逐步將 Spock 與 Spring 完全集成,仍然使用 Gradle 建置流程。
2. 將 Spock 與 Gradle 結合使用
我們需要建立一個 Gradle 專案並新增 Spock 依賴項。
2.1.設定 Gradle 項目
首先,讓我們在系統上安裝 Gradle 。然後可以使用gradle init
指令初始化Gradle 專案。例如,使用Java或 Kotlin 建立應用程式或程式庫有不同的選項。
無論如何,Gradle 專案始終會從以下位置取得配置:
-
build.gradle
。它包含有關建置過程的信息,例如 Java 版本或用於實作或測試的程式庫。我們將其稱為建置檔案。 -
settings.gradle
。它添加項目訊息,例如項目名稱或子模組結構。我們將其稱為設定檔。
Gradle 使用 JVM 外掛程式來實現專案的編譯、測試和捆綁功能。
如果我們選擇 Java,我們將透過使用' [java](https://docs.gradle.org/8.5/userguide/java_plugin.html#java_plugin) '
外掛程式來保持簡單,因為這是它最終將擴展的內容。
讓我們來看看 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'
我們需要在測試任務中使用useJUnitPlatform()
規格來執行測試。我們還添加了一些用於測試日誌記錄的屬性,以便我們在任務運行時獲得輸出:
test {
useJUnitPlatform()
testLogging {
events "started", "passed", "skipped", "failed"
}
}
我們也看到我們的專案如何使用mavenCentral()
儲存庫下載依賴項:
repositories {
mavenCentral()
}
值得注意的是,有些人可能會發現該配置比具有基於 XML 的pom.xml
建置配置的 Maven 專案更具可讀性。
最後,我們也看一下設定檔:
rootProject.name = 'spring-boot-testing-spock'
這非常簡單,只配置項目名稱。但是,它可以包含相關信息,例如子模組包含或插件定義。
我們可以檢查Gradle DSL 參考以取得有關build
或settings
腳本的更多資訊。
2.2.新增 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](https://mvnrepository.com/artifact/org.spockframework/spock-core/)
測試依賴項來更新我們的依賴項:
testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0'
值得注意的是,我們不需要像 Maven 專案那樣配置GMavenPlus插件。
2.3.運行測試
我們可以使用 Spock 進行不同類型的測試。讓我們來看一個簡單的測試案例:
class SpockTest extends Specification {
def "one plus one should equal two"() {
expect:
1 + 1 == 2
}
}
每個測試都必須擴展Specification
類別。此外,測試被定義為具有 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 使用快取系統,只會重新執行自上次執行以來發生變更的測試。
3. 使用 Spock、Gradle 和 Spring
我們可能想要將 Spock 新增到 Spring 專案中。 Spock 有一個專門用於此目的的模組。
讓我們來看看具有基本 Spring 配置的 Spock。稍後,我們還將了解 Spring Boot 設定。從現在開始,為了簡潔起見,我們將省略建置檔案中的java
和test
部分,因為它們不會改變。
3.1.史波克和斯普林
假設我們有一個 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'
3.2. 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'
3.3. 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
插件替換為dependecyManagent
部分,作為依賴項的單一入口點:
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',
)
4。結論
在本教程中,我們了解如何使用 Spock 和 Gradle 建立 Java 專案。我們也了解如何新增 Spring 和 Spring Boot 相依性。 Gradle 在我們的專案腳本設定中提供了強大的建置支援和更少的冗長。同樣,Spock 是一個很棒的測試工具,因為它易於設置和規範,面向數據驅動測試和基於交互,而不是 JUnit 的類似斷言形式。
與往常一樣,本文中提供的程式碼可用 在 GitHub 上。與往常一樣,本文中提供的程式碼可用 在 GitHub 上。