Cucumber Spring Integration
1.概述
Cucumber是使用Ruby编程语言编写的非常强大的测试框架,该框架遵循BDD(行为驱动的开发)方法。其目的是使开发人员能够以纯文本编写高级用例,并可由非技术利益相关者进行验证,并将其转换为可执行的测试,并使用一种名为Gherkin的语言编写。
我们已经在另一篇文章中讨论了这些。
Cucumber-Spring集成旨在简化测试自动化。一旦将Cucumber测试与Spring集成在一起,我们就应该能够与Maven构建一起执行它们。
2. Maven依赖
让我们通过定义Maven依赖关系开始使用Cucumber-Spring集成-从Cucumber-JVM依赖关系开始:
1 2 3 4 5 6 | <dependency> <groupId>info.cukes</groupId> cucumber-java</artifactId> <version>1.2.4</version> <scope>test</scope> </dependency> |
可以在此处找到Cucumber JVM的最新版本。
接下来,我们将添加JUnit和Cucumber测试依赖项:
1 2 3 4 5 6 | <dependency> <groupId>info.cukes</groupId> cucumber-junit</artifactId> <version>1.2.4</version> <scope>test</scope> </dependency> |
可以在这里找到Cucumber JUnit的最新版本。
最后,Spring和Cucumber的依赖关系:
1 2 3 4 5 6 | <dependency> <groupId>info.cukes</groupId> cucumber-spring</artifactId> <version>1.2.4</version> <scope>test</scope> </dependency> |
可以在这里找到Cucumber Spring的最新版本。
3.配置
现在,我们将研究如何将Cucumber集成到Spring Micro服务应用程序中。第一步是创建一个Spring Boot应用程序-我们将按照Spring-Boot应用程序文章进行操作。
然后,我们将在Boot应用程序中创建Spring REST服务,并为此REST服务编写Cucumber测试。
3.1。 REST控制器
首先,我们为简单的REST API创建一个控制器类:
1 2 3 4 5 6 7 | @RestController public class VersionController { @RequestMapping(method={RequestMethod.GET},value={"/version"}) public String getVersion() { return"1.0"; } } |
3.2。黄瓜步骤定义
JUnit运行器使用JUnit框架来运行Cucumber Test。我们需要做的是创建一个带有注释@RunWith(Cucumber.class)的空类:
1 2 3 4 | @RunWith(Cucumber.class) @CucumberOptions(features ="src/test/resources") public class CucumberTest { } |
我们可以看到注释@CucumberOptions,在其中指定小黄瓜文件(也称为功能文件)的位置。此时,黄瓜可以识别小黄瓜语言。您可以在引言中提到的文章中阅读有关小黄瓜的更多信息。
现在,让我们创建一个Cucumber功能文件:
1 2 3 4 5 | 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调用并声明响应。
下一步是在Java类中创建与该测试用例相对应的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @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)); } |
因此,现在我们需要使用Maven和Spring执行这些Cucumber测试用例方法。创建一个可以与Spring-JUnit一起运行的类,以便Maven可以执行那些测试类:
1 2 3 4 5 6 7 8 | @ContextConfiguration( classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class) @WebAppConfiguration @IntegrationTest public class SpringIntegrationTest { } |
现在,所有的Cucumber定义都可以进入一个单独的Java类,该类扩展到上述Java类:
1 2 3 4 5 6 7 | public class StepDefs extends SpringIntegrationTest { @When("^the client calls /version$") public void the_client_issues_GET_version() throws Throwable { executeGet("http://localhost:8080/version"); } } |
我们现在都准备进行测试。
我们可以通过命令行快速运行,只需运行mvnclean install -Pintegration– Maven将执行集成测试并在控制台中显示结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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。结论
使用Spring配置Cucumber之后,在BDD测试中使用Spring配置的组件将非常方便。这是将Cucumber测试集成到Spring-Boot应用程序中的简单指南。
您可以在链接的GitHub存储库中基于本文中的代码找到示例项目。