Skip to content

Commit

Permalink
added complete integration test with stubbing http-request
Browse files Browse the repository at this point in the history
  • Loading branch information
feech committed Apr 25, 2019
1 parent 506418c commit 0dd943f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions complete/pom.xml
Expand Up @@ -35,6 +35,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>

<dependency>
<groupId>org.anystub</groupId>
<artifactId>anystub</artifactId>
<version>0.2.22</version>

This comment has been minimized.

Copy link
@feech

feech Apr 28, 2019

Author

indeed you need add it in "test" scope

</dependency>
</dependencies>


Expand Down
35 changes: 35 additions & 0 deletions complete/src/test/java/hello/TestConfiguration.java
@@ -0,0 +1,35 @@
package hello;

import org.anystub.http.StubHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration

This comment has been minimized.

Copy link
@feech

feech Apr 28, 2019

Author

the only configuration of spring-container for all tests

public class TestConfiguration {

@Bean
public RestTemplateBuilder builder() {

This comment has been minimized.

Copy link
@feech

feech Apr 28, 2019

Author

spring-boot creates RestTemplateBuilder automatically, but for tests we have to create it manually to wrap the http-client with stubbing implementation


RestTemplateCustomizer restTemplateCustomizer = new RestTemplateCustomizer() {
@Override
public void customize(RestTemplate restTemplate) {
HttpClient real = HttpClientBuilder.create().build();
StubHttpClient stubHttpClient = new StubHttpClient(real);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(stubHttpClient);
restTemplate.setRequestFactory(requestFactory);
}
};

return new RestTemplateBuilder(restTemplateCustomizer);

}


}
8 changes: 8 additions & 0 deletions complete/src/test/resources/anystub/stub.yml
@@ -0,0 +1,8 @@
request0:
exception: []
keys: [GET, HTTP/1.1, 'https://gturnquist-quoters.cfapps.io/api/random']
values: [HTTP/1.1, '200', OK, 'Content-Type: application/json;charset=UTF-8', 'Date:
Thu, 25 Apr 2019 23:04:49 GMT', 'X-Vcap-Request-Id: 5ffce9f3-d972-4e95-6b5c-f88f9b0ae29b',
'Content-Length: 177', 'Connection: keep-alive', '{"type":"success","value":{"id":3,"quote":"Spring
has come quite a ways in addressing developer enjoyment and ease of use since
the last time I built an application using it."}}']

0 comments on commit 0dd943f

Please sign in to comment.