|
| 1 | +package org.baeldung.resttemplate; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.hamcrest.CoreMatchers.is; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import org.baeldung.config.RestClientConfig; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.http.HttpEntity; |
| 12 | +import org.springframework.http.HttpHeaders; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | +import org.springframework.web.client.RestTemplate; |
| 19 | + |
| 20 | +import com.baeldung.transfer.LoginForm; |
| 21 | + |
| 22 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 23 | +@ContextConfiguration(classes = RestClientConfig.class) |
| 24 | +public class RestTemplateIntegrationTest { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + RestTemplate restTemplate; |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() { |
| 31 | + LoginForm loginForm = new LoginForm("userName", "password"); |
| 32 | + HttpHeaders headers = new HttpHeaders(); |
| 33 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 34 | + HttpEntity<LoginForm> requestEntity = new HttpEntity<LoginForm>(loginForm, headers); |
| 35 | + |
| 36 | + ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class); |
| 37 | + |
| 38 | + assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK))); |
| 39 | + assertThat(responseEntity.getHeaders() |
| 40 | + .get("Foo") |
| 41 | + .get(0), is(equalTo("bar"))); |
| 42 | + } |
| 43 | +} |
0 commit comments