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

@@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public class SampleWebThymeleaf3ApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort
private int port;
@Test
public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port, String.class);
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Messages");
assertThat(entity.getBody()).doesNotContain("layout:fragment");
@@ -61,15 +64,14 @@ public class SampleWebThymeleaf3ApplicationTests {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("text", "FOO text");
map.set("summary", "FOO");
URI location = new TestRestTemplate()
.postForLocation("http://localhost:" + this.port, map);
URI location = this.restTemplate.postForLocation("/", map);
assertThat(location.toString()).contains("localhost:" + this.port);
}
@Test
public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class);
ResponseEntity<String> entity = this.restTemplate
.getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body");
}