|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.test.web.client.samples; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.springframework.core.io.ClassPathResource; |
| 21 | +import org.springframework.core.io.Resource; |
| 22 | +import org.springframework.http.HttpMethod; |
| 23 | +import org.springframework.http.MediaType; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.test.web.Person; |
| 26 | +import org.springframework.test.web.client.MockRestServiceServer; |
| 27 | +import org.springframework.util.concurrent.ListenableFuture; |
| 28 | +import org.springframework.web.client.AsyncRestTemplate; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; |
| 32 | +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; |
| 33 | +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; |
| 34 | + |
| 35 | +/** |
| 36 | + * Examples to demonstrate writing client-side REST tests with Spring MVC Test. |
| 37 | + * While the tests in this class invoke the RestTemplate directly, in actual |
| 38 | + * tests the RestTemplate may likely be invoked indirectly, i.e. through client |
| 39 | + * code. |
| 40 | + * |
| 41 | + * @author Rossen Stoyanchev |
| 42 | + */ |
| 43 | +public class SampleAsyncTests { |
| 44 | + |
| 45 | + private MockRestServiceServer mockServer; |
| 46 | + |
| 47 | + private AsyncRestTemplate restTemplate; |
| 48 | + |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setup() { |
| 52 | + this.restTemplate = new AsyncRestTemplate(); |
| 53 | + this.mockServer = MockRestServiceServer.createServer(this.restTemplate); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void performGet() throws Exception { |
| 59 | + |
| 60 | + String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; |
| 61 | + |
| 62 | + this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) |
| 63 | + .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON)); |
| 64 | + |
| 65 | + @SuppressWarnings("unused") |
| 66 | + ListenableFuture<ResponseEntity<Person>> ludwig = restTemplate.getForEntity("/composers/{id}", Person.class, 42); |
| 67 | + |
| 68 | + // We are only validating the request. The response is mocked out. |
| 69 | + // person.getName().equals("Ludwig van Beethoven") |
| 70 | + // person.getDouble().equals(1.6035) |
| 71 | + |
| 72 | + this.mockServer.verify(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void performGetAsync() throws Exception { |
| 77 | + |
| 78 | + String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; |
| 79 | + |
| 80 | + this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) |
| 81 | + .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON)); |
| 82 | + |
| 83 | + @SuppressWarnings("unused") |
| 84 | + ListenableFuture<ResponseEntity<Person>> ludwig = restTemplate.getForEntity("/composers/{id}", Person.class, 42); |
| 85 | + |
| 86 | + // person.getName().equals("Ludwig van Beethoven") |
| 87 | + // person.getDouble().equals(1.6035) |
| 88 | + |
| 89 | + this.mockServer.verify(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void performGetWithResponseBodyFromFile() throws Exception { |
| 94 | + |
| 95 | + Resource responseBody = new ClassPathResource("ludwig.json", this.getClass()); |
| 96 | + |
| 97 | + this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) |
| 98 | + .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON)); |
| 99 | + |
| 100 | + @SuppressWarnings("unused") |
| 101 | + ListenableFuture<ResponseEntity<Person>> ludwig = restTemplate.getForEntity("/composers/{id}", Person.class, 42); |
| 102 | + |
| 103 | + // hotel.getId() == 42 |
| 104 | + // hotel.getName().equals("Holiday Inn") |
| 105 | + |
| 106 | + this.mockServer.verify(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void verify() { |
| 111 | + |
| 112 | + this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) |
| 113 | + .andRespond(withSuccess("1", MediaType.TEXT_PLAIN)); |
| 114 | + |
| 115 | + this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) |
| 116 | + .andRespond(withSuccess("2", MediaType.TEXT_PLAIN)); |
| 117 | + |
| 118 | + this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) |
| 119 | + .andRespond(withSuccess("4", MediaType.TEXT_PLAIN)); |
| 120 | + |
| 121 | + this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) |
| 122 | + .andRespond(withSuccess("8", MediaType.TEXT_PLAIN)); |
| 123 | + |
| 124 | + @SuppressWarnings("unused") |
| 125 | + ListenableFuture<ResponseEntity<String>> result = this.restTemplate.getForEntity("/number", String.class); |
| 126 | + // result == "1" |
| 127 | + |
| 128 | + result = this.restTemplate.getForEntity("/number", String.class); |
| 129 | + // result == "2" |
| 130 | + |
| 131 | + try { |
| 132 | + this.mockServer.verify(); |
| 133 | + } |
| 134 | + catch (AssertionError error) { |
| 135 | + assertTrue(error.getMessage(), error.getMessage().contains("2 out of 4 were executed")); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments