Update the samples to make use of auto-configured TestRestTemplate
Closes gh-6730
This commit is contained in:
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
|
||||
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;
|
||||
@@ -49,8 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@DirtiesContext
|
||||
public class SampleUndertowApplicationTests {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void testHome() throws Exception {
|
||||
@@ -67,10 +67,8 @@ public class SampleUndertowApplicationTests {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept-Encoding", "gzip");
|
||||
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
|
||||
TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
ResponseEntity<byte[]> entity = restTemplate.exchange(
|
||||
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
|
||||
byte[].class);
|
||||
ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
|
||||
requestEntity, byte[].class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
GZIPInputStream inflater = new GZIPInputStream(
|
||||
new ByteArrayInputStream(entity.getBody()));
|
||||
@@ -84,8 +82,8 @@ public class SampleUndertowApplicationTests {
|
||||
}
|
||||
|
||||
private void assertOkResponse(String path, String body) {
|
||||
ResponseEntity<String> entity = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.port + path, String.class);
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity(path,
|
||||
String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).isEqualTo(body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user