Skip to content

Commit ea86701

Browse files
authored
Merge pull request eugenp#535 from skoiloth/master
Spring-cucumber-integration
2 parents 152d9b3 + 0cbfa6a commit ea86701

13 files changed

Lines changed: 385 additions & 0 deletions

‎spring-cucumber-demo/pom.xml‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-cucumber-demo</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
<cucumber.java.version>1.2.4</cucumber.java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>info.cukes</groupId>
41+
<artifactId>cucumber-core</artifactId>
42+
<version>${cucumber.java.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>info.cukes</groupId>
48+
<artifactId>cucumber-java</artifactId>
49+
<version>${cucumber.java.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>info.cukes</groupId>
55+
<artifactId>cucumber-junit</artifactId>
56+
<version>${cucumber.java.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>info.cukes</groupId>
62+
<artifactId>cucumber-spring</artifactId>
63+
<version>${cucumber.java.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
68+
<dependency>
69+
<groupId>org.apache.commons</groupId>
70+
<artifactId>commons-io</artifactId>
71+
<version>1.3.2</version>
72+
</dependency>
73+
74+
75+
</dependencies>
76+
77+
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-maven-plugin</artifactId>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
88+
89+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung;
2+
3+
import javax.servlet.http.HttpServletResponse;
4+
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class BaeldungController {
11+
12+
@RequestMapping(method={RequestMethod.GET},value={"/hello"})
13+
public String sayHello(HttpServletResponse response){
14+
return "hello";
15+
}
16+
17+
@RequestMapping(method={RequestMethod.POST},value={"/baeldung"})
18+
public String sayHelloPost(HttpServletResponse response){
19+
return "hello";
20+
}
21+
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.context.web.SpringBootServletInitializer;
7+
8+
@SpringBootApplication
9+
public class SpringDemoApplication extends SpringBootServletInitializer{
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringDemoApplication.class, args);
13+
}
14+
15+
@Override
16+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
17+
return application.sources(SpringDemoApplication.class);
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class VersionController {
9+
10+
@RequestMapping(method={RequestMethod.GET},value={"/version"})
11+
public String getVersion(){
12+
return "1.0";
13+
}
14+
}

‎spring-cucumber-demo/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung;
2+
3+
import cucumber.api.CucumberOptions;
4+
import cucumber.api.junit.Cucumber;
5+
import org.junit.runner.RunWith;
6+
7+
8+
@RunWith(Cucumber.class)
9+
@CucumberOptions(features = "src/test/resources")
10+
public class CucumberTest{
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung;
2+
3+
import org.springframework.http.HttpHeaders;
4+
import org.springframework.http.client.ClientHttpRequest;
5+
import org.springframework.web.client.RequestCallback;
6+
7+
import java.io.IOException;
8+
import java.util.Map;
9+
10+
11+
public class HeaderSettingRequestCallback implements RequestCallback{
12+
final Map<String,String> requestHeaders;
13+
14+
private String body;
15+
16+
public HeaderSettingRequestCallback(final Map<String, String> headers){
17+
this.requestHeaders = headers;
18+
}
19+
20+
public void setBody(final String postBody ){
21+
this.body = postBody;
22+
}
23+
24+
@Override
25+
public void doWithRequest(ClientHttpRequest request) throws IOException{
26+
final HttpHeaders clientHeaders = request.getHeaders();
27+
for( final Map.Entry<String,String> entry : requestHeaders.entrySet() ){
28+
clientHeaders.add(entry.getKey(),entry.getValue());
29+
}
30+
if( null != body ){
31+
request.getBody().write( body.getBytes() );
32+
}
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung;
2+
3+
import cucumber.api.java.en.Given;
4+
import cucumber.api.java.en.When;
5+
6+
7+
public class OtherDefs extends SpringIntegrationTest{
8+
@When("^the client calls /baeldung$")
9+
public void the_client_issues_POST_hello() throws Throwable{
10+
executePost("http://localhost:8080/baeldung");
11+
}
12+
13+
@Given("^the client calls /hello$")
14+
public void the_client_issues_GET_hello() throws Throwable{
15+
executeGet("http://localhost:8080/hello");
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
7+
import org.apache.commons.io.IOUtils;
8+
import org.springframework.http.client.ClientHttpResponse;
9+
10+
11+
public class ResponseResults{
12+
private final ClientHttpResponse theResponse;
13+
private final String body;
14+
15+
protected ResponseResults(final ClientHttpResponse response) throws IOException{
16+
this.theResponse = response;
17+
final InputStream bodyInputStream = response.getBody();
18+
if (null == bodyInputStream){
19+
this.body = "{}";
20+
}else{
21+
final StringWriter stringWriter = new StringWriter();
22+
IOUtils.copy(bodyInputStream, stringWriter);
23+
this.body = stringWriter.toString();
24+
}
25+
}
26+
27+
protected ClientHttpResponse getTheResponse(){
28+
return theResponse;
29+
}
30+
31+
protected String getBody(){
32+
return body;
33+
}
34+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.junit.runner.RunWith;
8+
import org.springframework.boot.test.IntegrationTest;
9+
import org.springframework.boot.test.SpringApplicationContextLoader;
10+
import org.springframework.http.HttpMethod;
11+
import org.springframework.http.client.ClientHttpResponse;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.web.WebAppConfiguration;
15+
import org.springframework.web.client.ResponseErrorHandler;
16+
import org.springframework.web.client.ResponseExtractor;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
20+
//@RunWith(SpringJUnit4ClassRunner.class)
21+
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
22+
@WebAppConfiguration
23+
@IntegrationTest
24+
public class SpringIntegrationTest {
25+
protected static ResponseResults latestResponse = null;
26+
27+
protected RestTemplate restTemplate = null;
28+
29+
protected void executeGet(String url) throws IOException{
30+
final Map<String,String> headers = new HashMap<>();
31+
headers.put("Accept","application/json");
32+
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
33+
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
34+
35+
if (restTemplate == null){
36+
restTemplate = new RestTemplate();
37+
}
38+
39+
restTemplate.setErrorHandler(errorHandler);
40+
latestResponse = restTemplate.execute(url,
41+
HttpMethod.GET,
42+
requestCallback,
43+
new ResponseExtractor<ResponseResults>(){
44+
@Override
45+
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
46+
if (errorHandler.hadError){
47+
return (errorHandler.getResults());
48+
} else{
49+
return (new ResponseResults(response));
50+
}
51+
}
52+
});
53+
54+
}
55+
56+
protected void executePost(String url) throws IOException{
57+
final Map<String,String> headers = new HashMap<>();
58+
headers.put("Accept","application/json");
59+
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
60+
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
61+
62+
if (restTemplate == null){
63+
restTemplate = new RestTemplate();
64+
}
65+
66+
restTemplate.setErrorHandler(errorHandler);
67+
latestResponse = restTemplate.execute(url,
68+
HttpMethod.POST,
69+
requestCallback,
70+
new ResponseExtractor<ResponseResults>(){
71+
@Override
72+
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
73+
if (errorHandler.hadError){
74+
return (errorHandler.getResults());
75+
} else{
76+
return (new ResponseResults(response));
77+
}
78+
}
79+
});
80+
81+
}
82+
83+
private class ResponseResultErrorHandler implements ResponseErrorHandler{
84+
private ResponseResults results = null;
85+
private Boolean hadError = false;
86+
87+
private ResponseResults getResults(){
88+
return results;
89+
}
90+
91+
@Override
92+
public boolean hasError(ClientHttpResponse response) throws IOException{
93+
hadError = response.getRawStatusCode() >= 400;
94+
return hadError;
95+
}
96+
97+
@Override
98+
public void handleError(ClientHttpResponse response) throws IOException {
99+
results = new ResponseResults(response);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)