Skip to content

Commit 75fe618

Browse files
committed
Fix broken (Async)RestTemplate integration tests
Issue: SPR-13157
1 parent e083683 commit 75fe618

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,27 +50,22 @@
5050
*/
5151
public class AbstractJettyServerTestCase {
5252

53-
protected static String helloWorld = "H\u00e9llo W\u00f6rld";
53+
protected static final String helloWorld = "H\u00e9llo W\u00f6rld";
5454

55-
protected static int port;
56-
protected static String baseUrl;
55+
protected static final int port = SocketUtils.findAvailableTcpPort();
5756

58-
protected static MediaType textContentType;
59-
protected static MediaType jsonContentType;
57+
protected static final String baseUrl = "http://localhost:" + port;
6058

61-
private static Server jettyServer;
59+
protected static final MediaType textContentType = new MediaType("text", "plain", Collections.singletonMap("charset", "utf-8"));
60+
61+
protected static final MediaType jsonContentType = new MediaType("application", "json", Collections.singletonMap("charset", "utf-8"));
62+
63+
private static final Server jettyServer = new Server(port);
6264

6365
@BeforeClass
6466
public static void startJettyServer() throws Exception {
65-
port = SocketUtils.findAvailableTcpPort();
66-
jettyServer = new Server(port);
67-
baseUrl = "http://localhost:" + port;
6867
ServletContextHandler handler = new ServletContextHandler();
69-
byte[] bytes = helloWorld.getBytes("UTF-8");
70-
textContentType = new MediaType("text", "plain", Collections
71-
.singletonMap("charset", "UTF-8"));
72-
jsonContentType = new MediaType("application", "json", Collections
73-
.singletonMap("charset", "UTF-8"));
68+
byte[] bytes = helloWorld.getBytes("utf-8");
7469
handler.addServlet(new ServletHolder(new GetServlet(bytes, textContentType)), "/get");
7570
handler.addServlet(new ServletHolder(new GetServlet(new byte[0], textContentType)), "/get/nothing");
7671
handler.addServlet(new ServletHolder(new GetServlet(bytes, null)), "/get/nocontenttype");
@@ -212,7 +207,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
212207
response.setStatus(HttpServletResponse.SC_CREATED);
213208
response.setHeader("Location", location);
214209
response.setContentType(contentType.toString());
215-
byte[] bytes = body.getBytes("UTF-8");
210+
byte[] bytes = body.getBytes("utf-8");
216211
response.setContentLength(bytes.length);;
217212
FileCopyUtils.copy(bytes, response.getOutputStream());
218213
}
@@ -244,7 +239,7 @@ private static class UriServlet extends HttpServlet {
244239
@Override
245240
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
246241
resp.setContentType("text/plain");
247-
resp.setCharacterEncoding("UTF-8");
242+
resp.setCharacterEncoding("utf-8");
248243
resp.getWriter().write(req.getRequestURI());
249244
}
250245
}

spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.Future;
2626
import java.util.concurrent.TimeUnit;
2727

28-
import org.junit.Before;
2928
import org.junit.Test;
3029

3130
import org.springframework.core.io.ClassPathResource;
@@ -50,13 +49,7 @@
5049
*/
5150
public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCase {
5251

53-
private AsyncRestTemplate template;
54-
55-
56-
@Before
57-
public void createTemplate() {
58-
template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory());
59-
}
52+
private final AsyncRestTemplate template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory());
6053

6154

6255
@Test

spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.EnumSet;
2424
import java.util.Set;
2525

26-
import com.fasterxml.jackson.annotation.JsonView;
27-
import org.junit.Before;
2826
import org.junit.Test;
2927

3028
import org.springframework.core.io.ClassPathResource;
@@ -40,19 +38,17 @@
4038
import org.springframework.util.LinkedMultiValueMap;
4139
import org.springframework.util.MultiValueMap;
4240

41+
import com.fasterxml.jackson.annotation.JsonView;
42+
4343
import static org.junit.Assert.*;
4444

4545
/**
4646
* @author Arjen Poutsma
4747
*/
4848
public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
4949

50-
private RestTemplate template;
50+
private final RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
5151

52-
@Before
53-
public void createTemplate() {
54-
template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
55-
}
5652

5753
@Test
5854
public void getString() {

0 commit comments

Comments
 (0)