Cucumber Spring Integration – Cucumber Spring集成

最后修改: 2016年 8月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Cucumber is a very powerful testing framework written in the Ruby programming language, which follows the BDD (behavior-driven development) methodology. It enables developers to write high-level use cases in plain text that can be verified by non-technical stakeholders, and turn them into executable tests, written in a language called Gherkin.

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

We have already discussed these in a different article.

我们已经在不同的文章中讨论过这些问题。

And the Cucumber-Spring Integration is intended to make test automation easier. Once we have the Cucumber tests integrated with Spring, we should be able to execute them along with the Maven build.

Cucumber-Spring集成是为了让测试自动化更容易。一旦我们将Cucumber测试与Spring集成,我们就应该能够与Maven构建一起执行这些测试。

2. Maven Dependencies

2.Maven的依赖性

Let’s get started using the Cucumber-Spring integration by defining the Maven dependencies – starting with the Cucumber-JVM dependency:

让我们开始使用Cucumber-Spring集成,定义Maven依赖项–从Cucumber-JVM依赖项开始。

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

We can find the most recent version of Cucumber JVM here.

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

Next, we’ll add the JUnit and Cucumber testing dependency:

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

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

The most recent version of Cucumber JUnit can be found here.

Cucumber JUnit的最新版本可以在这里找到。

And finally, the Spring and Cucumber dependency:

最后是Spring和黄瓜的依赖性。

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

Again, we can check out the most recent version of Cucumber Spring over here.

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

3. Configuration

3.配置

We’ll now look at how we can integrate Cucumber in a Spring application.

现在我们来看看如何在Spring应用中集成Cucumber。

First, we’ll create a Spring Boot application – for which we’ll follow the Spring-Boot application article. Then, we’ll create a Spring REST service and write the Cucumber test for it.

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

3.1. REST Controller

3.1.REST控制器

First, let’s create a simple controller:

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

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

3.2. Cucumber Step Definitions

3.2.黄瓜步骤的定义

All we need to run our Cucumber tests with JUnit is to create a single empty class with an annotation @RunWith(Cucumber.class):

我们用JUnit运行Cucumber测试所需要的只是创建一个带有注解的空类@RunWith(Cucumber.class)

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

We can see the annotation @CucumberOptions where we’re specifying the location of the Gherkin file which is also known as the feature file. At this point, Cucumber recognizes the Gherkin language; you can read more about Gherkin in the article mentioned in the introduction.

我们可以看到注释@CucumberOptions,在这里我们要指定Gherkin文件的位置,这也被称为特征文件。在这一点上,Cucumber识别了Gherkin语言;你可以在介绍中提到的文章中阅读更多关于Gherkin的内容。

So now, let’s create a Cucumber feature file:

那么现在,让我们来创建一个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

The Scenario is to make a GET call to the REST service url /version and verify the response.

场景是对REST服务网址/version进行GET调用并验证响应。

Next, we need to create a so-called glue code. These are methods that link a single Gherkin step with Java code.

接下来,我们需要创建一个所谓的胶水代码。这些是将单个小黄瓜步骤与Java代码联系起来的方法。

We have to options here – we can either use Cucumber Expressions or regular expressions inside the annotations. In our case, we’ll stick to the regular expressions:

我们在这里可以选择–我们可以使用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));
}

So now let’s integrate the Cucumber tests with the Spring Application Context. For that, we’ll create a new class and annotate it with @SpringBootTest and @CucumberContextConfiguration:

所以现在让我们将Cucumber测试与Spring应用上下文整合起来。为此,我们将创建一个新的类,并用@SpringBootTest@CucumberContextConfiguration来注释它。

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

Now all the Cucumber definitions can go into a separate Java class which extends SpringIntegrationTest:

现在,所有的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");
    }
}

We are all set for a test run now.

我们现在都准备好了,可以进行试运行。

Finally, we can do a quick run via the command line, simply running mvn clean install -Pintegration-lite-first – Maven will execute the integration tests and show the results in the console.

最后,我们可以通过命令行进行快速运行,只需运行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. Conclusion

4.结论

Having configured Cucumber with Spring, it will be handy to use Spring-configured components in BDD testing. This is a simple guide for integrating the Cucumber test in a Spring-Boot application.

在用Spring配置了Cucumber后,在BDD测试中使用Spring配置的组件会很方便。这是一个在Spring-Boot应用程序中集成Cucumber测试的简单指南。

As usual, all code samples shown in this tutorial are available over on GitHub.

像往常一样,本教程中显示的所有代码样本都可以在GitHub上找到