Contents

Spring 配置 Cucumber

1. 概述

Cucumber 是一个非常强大的测试框架,用 Ruby 编程语言编写,遵循 BDD(行为驱动开发)方法论。它使开发人员能够以纯文本形式编写高级用例,这些用例可以由非技术利益相关者验证,并将它们转化为可执行测试,用一种称为 Gherkin 的语言编写。

我们已经在另一篇文章 中讨论了这些。

Cucumber-Spring 集成 旨在使测试自动化更容易。一旦我们将 Cucumber 测试与 Spring 集成,我们应该能够与 Maven 构建一起执行它们。

2. Maven依赖

让我们通过定义 Maven 依赖项开始使用 Cucumber-Spring 集成——从 Cucumber-JVM 依赖项开始:

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

我们可以在这里 找到最新版本的 Cucumber JVM 。

接下来,我们将添加 JUnit 和 Cucumber 测试依赖项:

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

可以在此处 找到最新版本的 Cucumber JUnit 。 最后,Spring 和 Cucumber 依赖项:

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

同样,我们可以在这里 查看最新版本的 Cucumber Spring 。

3. 配置

现在我们将看看如何将 Cucumber 集成到 Spring 应用程序中。

首先,我们将创建一个 Spring Boot 应用程序——我们将遵循Spring-Boot 应用程序文章 。然后,我们将创建一个 Spring REST 服务并为其编写 Cucumber 测试。

3.1. 控制器

首先,让我们创建一个简单的控制器:

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

3.2. 步骤定义

使用 JUnit 运行 Cucumber 测试所需的只是创建一个带有注释*@RunWith(Cucumber.class)*的空类:

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

我们可以看到*@CucumberOptions*注释,其中我们指定了 Gherkin 文件的位置,该文件也称为功能文件。此时,Cucumber 识别了 Gherkin 语言;您可以在介绍中提到的文章中阅读有关 Gherkin 的更多信息。

所以现在,让我们创建一个 Cucumber 特征文件:

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 表达式 。在我们的例子中,我们将坚持使用正则表达式:

@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 定义都可以进入一个扩展SpringIntegrationTest的单独 Java 类:

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.blogdemo.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