forked from spring-guides/gs-consuming-rest
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added complete integration test with stubbing http-request
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Sorry, something went wrong. |
||
public class TestConfiguration { | ||
|
||
@Bean | ||
public RestTemplateBuilder builder() { | ||
This comment has been minimized.
Sorry, something went wrong.
feech
Author
|
||
|
||
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); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."}}'] |
indeed you need add it in "test" scope