知識庫 / Spring RSS 訂閱

使用 Spring 和 Spock 進行測試

Spring,Testing
HongKong
4
01:35 PM · Dec 06 ,2025

1. 引言

在本簡短教程中,我們將展示結合 Spring Boot 測試框架的強大支持以及 Spock 框架的表達能力,無論是在單元測試還是集成測試中。

2. 項目設置

讓我們從一個簡單的 Web 應用程序開始。它可以通過簡單的 REST 調用來問候、更改問候語並將其重置為默認值。除了主類之外,我們使用一個簡單的 <em >RestController</em >> 來提供功能:

@RestController
@RequestMapping("/hello")
public class WebController {

    @GetMapping
    public String salutation() {
        return "Hello world!";
    }
}

因此,控制器會用“Hello world!”來回應。@RestController註解和快捷註解確保了REST端點註冊。

3. Maven  依賴項:Spock 和 Spring Boot 測試

我們首先添加 Maven 依賴項以及如果需要,Maven 插件配置。

3.1. 使用 Spring 支持添加 Spock 框架依賴項

對於 Spock 本身 以及 Spring 支持,我們需要兩個依賴項:

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>2.4-M4-groovy-4.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>2.4-M4-groovy-4.0</version>
    <scope>test</scope>
</dependency>

請注意,版本號指的是所使用的 Groovy 版本。

3.2. 添加 Spring Boot Test

為了使用 Spring Boot Test 的測試實用程序,我們需要以下依賴項:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>3.2.1</version>
    <scope>test</scope>
</dependency>

3.3. 設置 Groovy

由於 Spock 基於 Groovy,我們需要添加並配置 gmavenplus-插件,以便在我們的測試中使用該語言:

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <goals>
                <goal>compileTests</goal>
            </goals>
        </execution>
    </executions>
</plugin>

請注意,由於我們僅需 Groovy 用於測試目的,因此我們限制了插件目標為 compileTest

4. 在 Spock 測試中加載 ApplicationContext

一個簡單的測試方法是 檢查 Spring 應用上下文中的所有 Bean 是否創建成功

@SpringBootTest
class LoadContextTest extends Specification {

    @Autowired (required = false)
    private WebController webController

    def "when context is loaded then all expected beans are created"() {
        expect: "the WebController is created"
        webController
    }
}

對於本次集成測試,我們需要啓動 ApplicationContext,這是 @SpringBootTest 為我們所做的事情。Spock 通過使用諸如 “when”, “then” 或 “expect” 等關鍵詞,在我們的測試中提供章節分隔。

此外,我們可以利用 Groovy Truth 來檢查 Bean 是否為空,如測試的最後一行所示。

5. 使用 WebMvcTest 在 Spock 測試中的應用

同樣,我們可以測試 WebController 的行為:

@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {

    @Autowired
    private MockMvc mvc

    def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
        expect: "Status is 200 and the response is 'Hello world!'"
        mvc.perform(get("/hello"))
          .andExpect(status().isOk())
          .andReturn()
          .response
          .contentAsString == "Hello world!"
    }
}

需要注意的是,在我們的Spock測試(或更準確地説,規範中,我們可使用熟悉的Spring Boot測試框架的所有常用註解,就像我們熟悉的那樣。

6. 結論

在本文中,我們解釋瞭如何設置 Maven 項目以使用 Spock 和 Spring Boot 測試框架。此外,我們還看到了這兩個框架如何完美地互補。

如果您想深入瞭解,請查看我們關於使用 Spring Boot 進行測試、Spock 框架和 Groovy 語言的教程。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.