Remove AsyncRestTemplate and related types
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,10 +23,10 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
import static org.springframework.http.HttpMethod.POST;
|
||||
import static org.springframework.test.web.client.ExpectedCount.once;
|
||||
import static org.springframework.test.web.client.ExpectedCount.twice;
|
||||
@@ -44,15 +44,15 @@ public class DefaultRequestExpectationTests {
|
||||
@Test
|
||||
public void match() throws Exception {
|
||||
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
|
||||
expectation.match(createRequest(GET, "/foo"));
|
||||
expectation.match(createRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchWithFailedExpectation() throws Exception {
|
||||
public void matchWithFailedExpectation() {
|
||||
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
|
||||
expectation.andExpect(method(POST));
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
|
||||
expectation.match(createRequest(GET, "/foo")))
|
||||
expectation.match(createRequest()))
|
||||
.withMessageContaining("Unexpected HttpMethod expected:<POST> but was:<GET>");
|
||||
}
|
||||
|
||||
@@ -81,10 +81,9 @@ public class DefaultRequestExpectationTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private ClientHttpRequest createRequest(HttpMethod method, String url) {
|
||||
private ClientHttpRequest createRequest() {
|
||||
try {
|
||||
return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(method, new URI(url));
|
||||
return new MockClientHttpRequest(HttpMethod.GET, new URI("/foo"));
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
@@ -42,7 +43,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||
*/
|
||||
public class UnorderedRequestExpectationManagerTests {
|
||||
|
||||
private UnorderedRequestExpectationManager manager = new UnorderedRequestExpectationManager();
|
||||
private final UnorderedRequestExpectationManager manager = new UnorderedRequestExpectationManager();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -57,7 +58,7 @@ public class UnorderedRequestExpectationManagerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroExpectedRequests() throws Exception {
|
||||
public void zeroExpectedRequests() {
|
||||
this.manager.verify();
|
||||
}
|
||||
|
||||
@@ -108,19 +109,18 @@ public class UnorderedRequestExpectationManagerTests {
|
||||
this.manager.validateRequest(createRequest(GET, "/bar"));
|
||||
this.manager.validateRequest(createRequest(GET, "/foo"));
|
||||
this.manager.validateRequest(createRequest(GET, "/foo"));
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
|
||||
this.manager.verify())
|
||||
.withMessageContaining("3 request(s) executed:\n" +
|
||||
"GET /bar\n" +
|
||||
"GET /foo\n" +
|
||||
"GET /foo\n");
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(this.manager::verify)
|
||||
.withMessageContaining("3 request(s) executed:\n" +
|
||||
"GET /bar\n" +
|
||||
"GET /foo\n" +
|
||||
"GET /foo\n");
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private ClientHttpRequest createRequest(HttpMethod method, String url) {
|
||||
try {
|
||||
return new org.springframework.mock.http.client.MockAsyncClientHttpRequest(method, new URI(url));
|
||||
return new MockClientHttpRequest(method, new URI(url));
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
@@ -31,7 +30,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -68,21 +66,12 @@ public class MockMvcClientHttpRequestFactoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
public void test() {
|
||||
RestTemplate template = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc));
|
||||
String result = template.getForObject("/foo", String.class);
|
||||
assertThat(result).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testAsyncTemplate() throws Exception {
|
||||
org.springframework.web.client.AsyncRestTemplate template = new org.springframework.web.client.AsyncRestTemplate(
|
||||
new MockMvcClientHttpRequestFactory(this.mockMvc));
|
||||
ListenableFuture<ResponseEntity<String>> entity = template.getForEntity("/foo", String.class);
|
||||
assertThat(entity.get().getBody()).isEqualTo("bar");
|
||||
}
|
||||
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.web.client.samples;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.web.Person;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Examples to demonstrate writing client-side REST tests with Spring MVC Test.
|
||||
* While the tests in this class invoke the RestTemplate directly, in actual
|
||||
* tests the RestTemplate may likely be invoked indirectly, i.e. through client
|
||||
* code.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class SampleAsyncTests {
|
||||
|
||||
private final org.springframework.web.client.AsyncRestTemplate restTemplate = new org.springframework.web.client.AsyncRestTemplate();
|
||||
|
||||
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
|
||||
|
||||
|
||||
@Test
|
||||
public void performGet() throws Exception {
|
||||
|
||||
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
|
||||
|
||||
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ListenableFuture<ResponseEntity<Person>> ludwig =
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
|
||||
// We are only validating the request. The response is mocked out.
|
||||
// person.getName().equals("Ludwig van Beethoven")
|
||||
// person.getDouble().equals(1.6035)
|
||||
|
||||
this.mockServer.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void performGetManyTimes() throws Exception {
|
||||
|
||||
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
|
||||
|
||||
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ListenableFuture<ResponseEntity<Person>> ludwig =
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
|
||||
// We are only validating the request. The response is mocked out.
|
||||
// person.getName().equals("Ludwig van Beethoven")
|
||||
// person.getDouble().equals(1.6035)
|
||||
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
|
||||
this.mockServer.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void performGetWithResponseBodyFromFile() throws Exception {
|
||||
|
||||
Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
|
||||
|
||||
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ListenableFuture<ResponseEntity<Person>> ludwig =
|
||||
this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
|
||||
|
||||
// hotel.getId() == 42
|
||||
// hotel.getName().equals("Holiday Inn")
|
||||
|
||||
this.mockServer.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verify() {
|
||||
|
||||
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess("1", MediaType.TEXT_PLAIN));
|
||||
|
||||
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess("2", MediaType.TEXT_PLAIN));
|
||||
|
||||
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess("4", MediaType.TEXT_PLAIN));
|
||||
|
||||
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ListenableFuture<ResponseEntity<String>> result = this.restTemplate.getForEntity("/number", String.class);
|
||||
// result == "1"
|
||||
|
||||
result = this.restTemplate.getForEntity("/number", String.class);
|
||||
// result == "2"
|
||||
|
||||
try {
|
||||
this.mockServer.verify();
|
||||
}
|
||||
catch (AssertionError error) {
|
||||
assertThat(error.getMessage().contains("2 unsatisfied expectation(s)")).as(error.getMessage()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user