From 073e78b68d1a9d7b63cdd819d1fb12c72f352eb5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 25 Feb 2018 15:04:19 +0100 Subject: [PATCH] Polishing --- .../web/servlet/setup/MockMvcBuilders.java | 6 +----- .../setup/StandaloneMockMvcBuilder.java | 20 ++++++++----------- .../DelegatingWebConnectionTests.java | 12 +++++------ .../standalone/ExceptionHandlerTests.java | 12 +++++------ .../setup/StandaloneMockMvcBuilderTests.java | 7 +++---- 5 files changed, 23 insertions(+), 34 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcBuilders.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcBuilders.java index 6522ad1ddd7..0f408fbb897 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcBuilders.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcBuilders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -51,24 +51,20 @@ public class MockMvcBuilders { * Build a {@link MockMvc} instance by registering one or more * {@code @Controller} instances and configuring Spring MVC infrastructure * programmatically. - * *

This allows full control over the instantiation and initialization of * controllers and their dependencies, similar to plain unit tests while * also making it possible to test one controller at a time. - * *

When this builder is used, the minimum infrastructure required by the * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} * to serve requests with annotated controllers is created automatically * and can be customized, resulting in configuration that is equivalent to * what MVC Java configuration provides except using builder-style methods. - * *

If the Spring MVC configuration of an application is relatively * straight-forward — for example, when using the MVC namespace in * XML or MVC Java config — then using this builder might be a good * option for testing a majority of controllers. In such cases, a much * smaller number of tests can be used to focus on testing and verifying * the actual Spring MVC configuration. - * * @param controllers one or more {@code @Controller} instances to test */ public static StandaloneMockMvcBuilder standaloneSetup(Object... controllers) { diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java index 2b88427692b..d85503118dd 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -32,8 +32,6 @@ import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.FormattingConversionService; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.mock.web.MockServletContext; -import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; import org.springframework.util.StringValueResolver; @@ -127,18 +125,16 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilderNormally {@code @ControllerAdvice} are auto-detected as long as they're - * declared as Spring beans. However since the standalone setup does not load - * any Spring configuration they need to be registered explicitly here - * instead much like controllers. + * Register one or more {@link org.springframework.web.bind.annotation.ControllerAdvice} + * instances to be used in tests. + *

Normally {@code @ControllerAdvice} are auto-detected as long as they're declared + * as Spring beans. However since the standalone setup does not load any Spring config, + * they need to be registered explicitly here instead much like controllers. * @since 4.2 */ public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice) { @@ -531,7 +527,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder emptyList()); + WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.emptyList()); expectedResponse = new WebResponse(data, request, 100L); webConnection = new DelegatingWebConnection(defaultConnection, new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2)); @@ -132,14 +131,13 @@ public class DelegatingWebConnectionTests { WebClient webClient = new WebClient(); - MockMvc mockMvc = MockMvcBuilders.standaloneSetup(TestController.class).build(); + MockMvc mockMvc = MockMvcBuilders.standaloneSetup().build(); MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient); WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); WebConnection httpConnection = new HttpWebConnection(webClient); - WebConnection webConnection = new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection)); - - webClient.setWebConnection(webConnection); + webClient.setWebConnection( + new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection))); Page page = webClient.getPage("http://code.jquery.com/jquery-1.11.0.min.js"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java index ca098b6fe73..5ae2d5a9baa 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -16,10 +16,6 @@ package org.springframework.test.web.servlet.samples.standalone; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; - import org.junit.Test; import org.springframework.stereotype.Controller; @@ -29,6 +25,10 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; + /** * Exception handling via {@code @ExceptionHandler} method. * @@ -36,7 +36,6 @@ import org.springframework.web.bind.annotation.RequestMethod; */ public class ExceptionHandlerTests { - @Test public void testExceptionHandlerMethod() throws Exception { standaloneSetup(new PersonController()).build() @@ -74,6 +73,7 @@ public class ExceptionHandlerTests { } } + @ControllerAdvice private static class GlobalExceptionHandler { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java index d8146dc7d22..9656b46ec74 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -43,7 +43,7 @@ import static org.junit.Assert.*; /** * Tests for {@link StandaloneMockMvcBuilder} * - * @author Rossen + * @author Rossen Stoyanchev * @author Rob Winch * @author Sebastien Deleuze */ @@ -87,8 +87,7 @@ public class StandaloneMockMvcBuilderTests { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController()); builder.addPlaceholderValue("sys.login.ajax", "/foo"); WebApplicationContext wac = builder.initWebAppContext(); - assertEquals(wac, WebApplicationContextUtils - .getRequiredWebApplicationContext(wac.getServletContext())); + assertEquals(wac, WebApplicationContextUtils.getRequiredWebApplicationContext(wac.getServletContext())); } @Test(expected = IllegalArgumentException.class)