我正在研究Cucumber与Spring Boot应用程序的集成,以测试我的Rest Web服务。
当我运行应用程序,然后在项目中运行mvn test
时,它对我来说很好用。但这不是我所需要的。实际上,我希望它对我来说更加自动化。
我想仅使用 命令mvn clean install
来运行测试。但是此命令直接运行测试而不运行应用程序,因此测试总是失败“连接被拒绝” ,这是正常的,因为应用程序未运行。
我的问题是当我使用命令mvn clean install
为此,我使用spring initializr创建了一个简单的spring boot应用程序(仅添加了web)。 之后,我添加了黄瓜依赖项:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
所以我的pom.xml看起来像这样:
http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0
<groupId>cucumber</groupId>
<artifactId>hellocucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hellocucumber</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后我创建了一个控制器来用黄瓜对其进行测试:
@RestController
public class VersionController {
@GetMapping(path="/version")
public String getVersion() {
return "1.0";
}
}
对于黄瓜配置,我创建了此类
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/features/", plugin = "pretty")
public class HelloCucumberCucumberRunnerTest {
}
我也创建了我的功能文件
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"
步骤定义类:
@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class VersionSteps{
@Autowired
RestTemplate restTemplate;
@Value("${app.url}")
String url;
ResponseEntity<String> responseEntity;
@When("^the client calls /version$")
public void the_client_calls_version() throws Throwable {
url += "/version";
this.responseEntity = restTemplate.getForEntity(url, String.class);
}
@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int serverStatus) throws Throwable {
assertEquals(responseEntity.getStatusCode().value(), serverStatus);
}
@Then("^the client receives server version \"([^\"]*)\"$")
public void the_client_receives_server_version(String version) throws Throwable {
assertEquals(responseEntity.getBody(), version);
}