Cucumber fix (#2765)

* Rest-assured fix

* Cucumber fix

* HystrixManualTest

* Reformat HystrixTimeoutManualTest
This commit is contained in:
Grzegorz Piwowarek 2017-10-19 22:10:21 +02:00 committed by GitHub
parent aed9c8bfa8
commit 8616b07506
6 changed files with 66 additions and 78 deletions

View File

@ -2,7 +2,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>hystrix</artifactId> <artifactId>hystrix</artifactId>
<version>1.0</version> <version>1.0</version>
<name>hystrix</name> <name>hystrix</name>

View File

@ -10,10 +10,10 @@ import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class HystrixTimeoutIntegrationTest { public class HystrixTimeoutManualTest {
@Test @Test
public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob(){ public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob() {
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!")); assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
} }

View File

@ -1,16 +0,0 @@
package com.baeldung;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
public class OtherDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /baeldung$")
public void the_client_issues_POST_hello() throws Throwable {
executePost("http://localhost:8082/baeldung");
}
@Given("^the client calls /hello$")
public void the_client_issues_GET_hello() throws Throwable {
executeGet("http://localhost:8082/hello");
}
}

View File

@ -11,23 +11,19 @@ public class ResponseResults {
private final ClientHttpResponse theResponse; private final ClientHttpResponse theResponse;
private final String body; private final String body;
protected ResponseResults(final ClientHttpResponse response) throws IOException { ResponseResults(final ClientHttpResponse response) throws IOException {
this.theResponse = response; this.theResponse = response;
final InputStream bodyInputStream = response.getBody(); final InputStream bodyInputStream = response.getBody();
if (null == bodyInputStream) {
this.body = "{}";
} else {
final StringWriter stringWriter = new StringWriter(); final StringWriter stringWriter = new StringWriter();
IOUtils.copy(bodyInputStream, stringWriter); IOUtils.copy(bodyInputStream, stringWriter);
this.body = stringWriter.toString(); this.body = stringWriter.toString();
} }
}
protected ClientHttpResponse getTheResponse() { ClientHttpResponse getTheResponse() {
return theResponse; return theResponse;
} }
protected String getBody() { String getBody() {
return body; return body;
} }
} }

View File

@ -1,9 +1,5 @@
package com.baeldung; package com.baeldung;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationContextLoader; import org.springframework.boot.test.SpringApplicationContextLoader;
@ -12,20 +8,23 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class) @ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest
public class SpringIntegrationTest { public class SpringIntegrationTest {
protected static ResponseResults latestResponse = null; static ResponseResults latestResponse = null;
@Autowired @Autowired
protected RestTemplate restTemplate; protected RestTemplate restTemplate;
protected void executeGet(String url) throws IOException { void executeGet(String url) throws IOException {
final Map<String, String> headers = new HashMap<>(); final Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json"); headers.put("Accept", "application/json");
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@ -39,10 +38,9 @@ public class SpringIntegrationTest {
return (new ResponseResults(response)); return (new ResponseResults(response));
} }
}); });
} }
protected void executePost(String url) throws IOException { void executePost() throws IOException {
final Map<String, String> headers = new HashMap<>(); final Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json"); headers.put("Accept", "application/json");
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@ -53,14 +51,14 @@ public class SpringIntegrationTest {
} }
restTemplate.setErrorHandler(errorHandler); restTemplate.setErrorHandler(errorHandler);
latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, response -> { latestResponse = restTemplate
.execute("http://localhost:8082/baeldung", HttpMethod.POST, requestCallback, response -> {
if (errorHandler.hadError) { if (errorHandler.hadError) {
return (errorHandler.getResults()); return (errorHandler.getResults());
} else { } else {
return (new ResponseResults(response)); return (new ResponseResults(response));
} }
}); });
} }
private class ResponseResultErrorHandler implements ResponseErrorHandler { private class ResponseResultErrorHandler implements ResponseErrorHandler {

View File

@ -3,6 +3,7 @@ package com.baeldung;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import cucumber.api.java.en.Given;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import cucumber.api.java.en.And; import cucumber.api.java.en.And;
@ -11,6 +12,16 @@ import cucumber.api.java.en.When;
public class StepDefsIntegrationTest extends SpringIntegrationTest { public class StepDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /baeldung$")
public void the_client_issues_POST_hello() throws Throwable {
executePost();
}
@Given("^the client calls /hello$")
public void the_client_issues_GET_hello() throws Throwable {
executeGet("http://localhost:8082/hello");
}
@When("^the client calls /version$") @When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable { public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8082/version"); executeGet("http://localhost:8082/version");