Deprecate secure flag on @WebMvcTest

Closes gh-14227
This commit is contained in:
Madhura Bhave
2018-08-29 16:52:05 -07:00
parent 174f53e1cf
commit d91c71b508
18 changed files with 58 additions and 16 deletions

View File

@@ -89,7 +89,9 @@ public @interface AutoConfigureMockMvc {
* If Spring Security's {@link MockMvc} support should be auto-configured when it is
* on the classpath. Defaults to {@code true}.
* @return if Spring Security's MockMvc support is auto-configured
* @deprecated since 2.1.0 in favor of Spring Security's testing support
*/
@Deprecated
boolean secure() default true;
}

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.test.autoconfigure.web.servlet;
import java.util.concurrent.Executors;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
@@ -29,8 +31,10 @@ import org.springframework.boot.test.web.htmlunit.webdriver.LocalHostWebConnecti
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.concurrent.DelegatingSecurityContextExecutor;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
import org.springframework.util.ClassUtils;
/**
* Auto-configuration for Selenium {@link WebDriver} MockMVC integration.
@@ -46,6 +50,8 @@ public class MockMvcWebDriverAutoConfiguration {
private final Environment environment;
private static final String SECURITY_CONTEXT_EXECUTOR = "org.springframework.security.concurrent.DelegatingSecurityContextExecutor";
MockMvcWebDriverAutoConfiguration(Environment environment) {
this.environment = environment;
}
@@ -63,7 +69,13 @@ public class MockMvcWebDriverAutoConfiguration {
@ConditionalOnMissingBean(WebDriver.class)
@ConditionalOnBean(MockMvcHtmlUnitDriverBuilder.class)
public HtmlUnitDriver htmlUnitDriver(MockMvcHtmlUnitDriverBuilder builder) {
return builder.build();
HtmlUnitDriver driver = builder.build();
if (ClassUtils.isPresent(SECURITY_CONTEXT_EXECUTOR,
getClass().getClassLoader())) {
driver.setExecutor(new DelegatingSecurityContextExecutor(
Executors.newSingleThreadExecutor()));
}
return driver;
}
}

View File

@@ -143,7 +143,9 @@ public @interface WebMvcTest {
* If Spring Security's {@link MockMvc} support should be auto-configured when it is
* on the classpath. Defaults to {@code true}.
* @return if Spring Security's MockMvc support is auto-configured
* @deprecated since 2.1.0 in favor of Spring Security's testing support
*/
@Deprecated
@AliasFor(annotation = AutoConfigureMockMvc.class)
boolean secure() default true;

View File

@@ -49,7 +49,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
* @author Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = RestDocsTestController.class, secure = false)
@WebMvcTest(controllers = RestDocsTestController.class)
@AutoConfigureRestDocs
public class MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests {

View File

@@ -26,6 +26,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -42,7 +43,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
public class WebMvcTestAllControllersIntegrationTests {
@Rule

View File

@@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -36,7 +37,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ExampleController2.class, secure = false)
@WebMvcTest(controllers = ExampleController2.class)
@WithMockUser
public class WebMvcTestConverterIntegrationTests {
@Autowired

View File

@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -36,7 +37,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
@TestPropertySource(properties = { "spring.mvc.throw-exception-if-no-handler-found=true",
"spring.mvc.static-path-pattern=/static/**" })
public class WebMvcTestCustomDispatcherServletIntegrationTests {

View File

@@ -22,6 +22,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpHeaders;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -34,7 +35,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Andy Wilkinson
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
public class WebMvcTestHateoasIntegrationTests {
@Autowired

View File

@@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -36,7 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
@TestPropertySource(properties = "spring.messages.basename=web-test-messages")
public class WebMvcTestMessageSourceIntegrationTests {

View File

@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -34,7 +35,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ExampleController2.class, secure = false)
@WebMvcTest(controllers = ExampleController2.class)
@WithMockUser
public class WebMvcTestOneControllerIntegrationTests {
@Autowired

View File

@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -34,7 +35,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
public class WebMvcTestPageableIntegrationTests {
@Autowired

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -39,7 +40,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureMockMvc(secure = false, printOnlyOnFailure = false)
@WithMockUser
@AutoConfigureMockMvc(printOnlyOnFailure = false)
public class WebMvcTestPrintAlwaysIntegrationTests {
@Rule

View File

@@ -22,6 +22,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -35,7 +36,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@RunWith(WebMvcTestPrintDefaultRunner.class)
@WebMvcTest
@AutoConfigureMockMvc(secure = false)
@WithMockUser
@AutoConfigureMockMvc
public class WebMvcTestPrintDefaultIntegrationTests {
@Autowired

View File

@@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -38,7 +39,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
@TestPropertySource(properties = "spring.test.mockmvc.print=NONE")
public class WebMvcTestPrintDefaultOverrideIntegrationTests {

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -40,7 +41,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureMockMvc(secure = false, print = MockMvcPrint.NONE)
@WithMockUser
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
public class WebMvcTestPrintOverrideIntegrationTests {
@Rule

View File

@@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,7 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
public class WebMvcTestWebClientIntegrationTests {
@Autowired

View File

@@ -41,7 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WebMvcTestWebDriverCustomScopeIntegrationTests {

View File

@@ -27,6 +27,7 @@ import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
@@ -39,7 +40,8 @@ import static org.junit.Assert.fail;
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@WebMvcTest
@WithMockUser
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WebMvcTestWebDriverIntegrationTests {