|
| 1 | +package org.baeldung.client; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.test.context.junit4.SpringRunner; |
| 11 | +import org.springframework.test.web.client.MockRestServiceServer; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; |
| 15 | +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; |
| 16 | + |
| 17 | +@RunWith(SpringRunner.class) |
| 18 | +@RestClientTest(DetailsServiceClient.class) |
| 19 | +public class DetailsServiceClientTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private DetailsServiceClient client; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private MockRestServiceServer server; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private ObjectMapper objectMapper; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setUp() throws Exception { |
| 32 | + String detailsString = objectMapper.writeValueAsString(new Details("John Smith", "john")); |
| 33 | + this.server.expect(requestTo("/john/details")) |
| 34 | + .andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenCallingGetUserDetails_thenClientExecutesCorrectCall() throws Exception { |
| 39 | + |
| 40 | + Details details = this.client.getUserDetails("john"); |
| 41 | + |
| 42 | + assertThat(details.getLogin()).isEqualTo("john"); |
| 43 | + assertThat(details.getName()).isEqualTo("John Smith"); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments