Update the samples to make use of auto-configured TestRestTemplate

Closes gh-6730
This commit is contained in:
Andy Wilkinson
2016-08-24 10:12:37 +01:00
parent 3c5cf02882
commit 07a50bb16c
47 changed files with 375 additions and 421 deletions

View File

@@ -16,20 +16,19 @@
package sample.hypermedia;
import java.net.URI;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@@ -39,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleHypermediaApplicationHomePageTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void home() {
String response = new TestRestTemplate()
.getForObject("http://localhost:" + this.port, String.class);
String response = this.restTemplate.getForObject("/", String.class);
assertThat(response).contains("404");
}
@@ -53,10 +51,8 @@ public class SampleHypermediaApplicationHomePageTests {
public void linksWithJson() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
ResponseEntity<String> response = new TestRestTemplate().exchange(
new RequestEntity<Void>(headers, HttpMethod.GET,
new URI("http://localhost:" + this.port + "/actuator")),
String.class);
ResponseEntity<String> response = this.restTemplate.exchange("/actuator",
HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
assertThat(response.getBody()).contains("\"_links\":");
}
@@ -64,10 +60,8 @@ public class SampleHypermediaApplicationHomePageTests {
public void halWithHtml() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> response = new TestRestTemplate().exchange(
new RequestEntity<Void>(headers, HttpMethod.GET,
new URI("http://localhost:" + this.port + "/actuator/")),
String.class);
ResponseEntity<String> response = this.restTemplate.exchange("/actuator/",
HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
assertThat(response.getBody()).contains("HAL Browser");
}