知識庫 / Spring RSS 訂閱

Cucumber 與 Spring Integration

Spring,Testing
HongKong
3
02:48 PM · Dec 06 ,2025

1. 概述

Cucumber 是一個強大的測試框架,使用 Ruby 編程語言編寫,遵循 BDD(行為驅動開發)方法論。它允許開發者編寫高層次的用例,這些用例可以使用純文本進行驗證,並由非技術利益相關者進行驗證,然後將其轉換為可執行的測試,這些測試使用一種名為 Gherkin 的語言編寫。

我們之前在另一篇文章中已經討論過這些內容。

並且 Cucumber-Spring 集成 的目的是使測試自動化更容易。一旦我們將 Cucumber 測試與 Spring 集成,我們應該能夠與 Maven 構建一起執行它們。

2. Maven 依賴

讓我們開始使用 Cucumber-Spring 集成,通過定義 Maven 依賴項來實現。首先,定義 Cucumber-JVM 依賴項:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.14.0</version>
    <scope>test</scope>
</dependency>

我們可以在 這裏找到 Cucumber JVM 的最新版本。

接下來,我們將添加 JUnit 和 Cucumber 測試依賴項:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>7.14.0</version>
    <scope>test</scope>
</dependency>

最新版本的 Cucumber JUnit 可以找到 這裏

並且,Spring 和 Cucumber 依賴項:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>7.14.0</version>
    <scope>test</scope>
</dependency>

再次,我們可以查看 Cucumber Spring 的最新版本,地址請參考 這裏

3. 配置

我們將探討如何在 Spring 應用中集成 Cucumber。

首先,我們將創建一個 Spring Boot 應用——我們將遵循 Spring-Boot 應用文章中的步驟。然後,我們將創建一個 Spring REST 服務,併為它編寫 Cucumber 測試。

3.1. REST 控制器

首先,讓我們創建一個簡單的控制器:

@RestController
public class VersionController {
    @GetMapping("/version")
    public String getVersion() {
        return "1.0";
    }
}

3.2. Cucumber 步驟定義

我們需要運行 Cucumber 測試與 JUnit 結合時,只需創建一個空類並添加一個註解 <em @RunWith(Cucumber.class):

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest {
}

我們來看一下注釋 @CucumberOptions,其中我們指定了 Gherkin 文件的位置,也稱為 feature 文件。 在這一步,Cucumber 識別 Gherkin 語言;您可以在介紹中提到的文章中瞭解更多關於 Gherkin 的信息。

現在,讓我們創建一個 Cucumber feature 文件:

Feature: the version can be retrieved
  Scenario: client makes call to GET /version
    When the client calls /version
    Then the client receives status code of 200
    And the client receives server version 1.0

場景是向 REST 服務 URL /version 發送 GET 請求,並驗證響應。

接下來,我們需要創建所謂的“粘合代碼”。這些是連接單個 Gherkin 步驟與 Java 代碼的方法。

這裏有兩條選擇——我們可以使用 Cucumber Expressions 或在註解中使用的正則表達式。 在我們的情況下,我們將堅持使用正則表達式:

@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable{
    executeGet("http://localhost:8080/version");
}

@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
    HttpStatus currentStatusCode = latestResponse.getTheResponse().getStatusCode();
    assertThat("status code is incorrect : "+ 
    latestResponse.getBody(), currentStatusCode.value(), is(statusCode));
}

@And("^the client receives server version (.+)$")
public void the_client_receives_server_version_body(String version) throws Throwable {
    assertThat(latestResponse.getBody(), is(version));
}

現在,讓我們將 Cucumber 測試與 Spring Application Context 集成。為此,我們將創建一個新的類並使用 @SpringBootTest@CucumberContextConfiguration 標註它:

@CucumberContextConfiguration
@SpringBootTest
public class SpringIntegrationTest {
    // executeGet implementation
}

現在所有Cucumber定義都可以放入一個單獨的Java類,該類繼承SpringIntegrationTest

public class StepDefs extends SpringIntegrationTest {
   
    @When("^the client calls /version$")
    public void the_client_issues_GET_version() throws Throwable {
        executeGet("http://localhost:8080/version");
    }
}

我們現在將進行測試運行。

最後,我們可以通過命令行進行快速測試,只需運行 mvn clean install -Pintegration-lite-first —— Maven 將執行集成測試並顯示結果在控制枱中。

3 Scenarios ([32m3 passed[0m)
9 Steps ([32m9 passed[0m)
0m1.054s

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 9.283 sec - in
  com.baeldung.CucumberTest
2016-07-30 06:28:20.142  INFO 732 --- [Thread-2] AnnotationConfigEmbeddedWebApplicationContext :
  Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext:
  startup date [Sat Jul 30 06:28:12 CDT 2016]; root of context hierarchy

Results :

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0

4. 結論

已配置 Cucumber 與 Spring 後,使用 Spring 配置的組件在 BDD 測試中將非常方便。 這是一份用於將 Cucumber 測試集成到 Spring-Boot 應用程序中的簡單指南。

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

發佈 評論

Some HTML is okay.