Turn registered controller classes into controller instances on the fly
Issue: SPR-16520
This commit is contained in:
@@ -33,7 +33,7 @@ import org.springframework.web.reactive.config.PathMatchConfigurer;
|
||||
import org.springframework.web.reactive.config.ViewResolverRegistry;
|
||||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultControllerSpec}.
|
||||
@@ -63,19 +63,19 @@ public class DefaultControllerSpecTests {
|
||||
.expectBody(String.class).isEqualTo("Handled exception");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void controllerIsAnObjectInstance() {
|
||||
new DefaultControllerSpec(MyController.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void controllerAdviceIsAnObjectInstance() {
|
||||
new DefaultControllerSpec(new MyController()).controllerAdvice(MyControllerAdvice.class);
|
||||
@Test
|
||||
public void controllerAdviceWithClassArgument() {
|
||||
new DefaultControllerSpec(MyController.class)
|
||||
.controllerAdvice(MyControllerAdvice.class)
|
||||
.build()
|
||||
.get().uri("/exception")
|
||||
.exchange()
|
||||
.expectStatus().isBadRequest()
|
||||
.expectBody(String.class).isEqualTo("Handled exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurerConsumers() {
|
||||
|
||||
TestConsumer<ArgumentResolverConfigurer> argumentResolverConsumer = new TestConsumer<>();
|
||||
TestConsumer<RequestedContentTypeResolverBuilder> contenTypeResolverConsumer = new TestConsumer<>();
|
||||
TestConsumer<CorsRegistry> corsRegistryConsumer = new TestConsumer<>();
|
||||
@@ -104,6 +104,7 @@ public class DefaultControllerSpecTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
private static class MyController {
|
||||
|
||||
@@ -119,6 +120,7 @@ public class DefaultControllerSpecTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ControllerAdvice
|
||||
private static class MyControllerAdvice {
|
||||
|
||||
@@ -128,11 +130,11 @@ public class DefaultControllerSpecTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestConsumer<T> implements Consumer<T> {
|
||||
|
||||
private T value;
|
||||
|
||||
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -26,7 +26,6 @@ import com.gargoylesoftware.htmlunit.WebConnection;
|
||||
import com.gargoylesoftware.htmlunit.WebRequest;
|
||||
import com.gargoylesoftware.htmlunit.WebResponse;
|
||||
import com.gargoylesoftware.htmlunit.WebResponseData;
|
||||
import com.gargoylesoftware.htmlunit.util.NameValuePair;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -82,7 +81,7 @@ public class DelegatingWebConnectionTests {
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
request = new WebRequest(new URL("http://localhost/"));
|
||||
WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.<NameValuePair> 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));
|
||||
|
||||
@@ -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()
|
||||
@@ -53,6 +52,14 @@ public class ExceptionHandlerTests {
|
||||
.andExpect(forwardedUrl("globalErrorView"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGlobalExceptionHandlerMethodUsingClassArgument() throws Exception {
|
||||
standaloneSetup(PersonController.class).setControllerAdvice(GlobalExceptionHandler.class).build()
|
||||
.perform(get("/person/Bonnie"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(forwardedUrl("globalErrorView"));
|
||||
}
|
||||
|
||||
|
||||
@Controller
|
||||
private static class PersonController {
|
||||
@@ -74,6 +81,7 @@ public class ExceptionHandlerTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ControllerAdvice
|
||||
private static class GlobalExceptionHandler {
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -125,17 +124,6 @@ public class StandaloneMockMvcBuilderTests {
|
||||
assertNotNull(serializer);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void controllerIsAnObjectInstance() {
|
||||
new StandaloneMockMvcBuilder(PersonController.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void controllerAdviceIsAnObjectInstance() {
|
||||
new StandaloneMockMvcBuilder(new PersonController()).setControllerAdvice(PersonController.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Controller
|
||||
private static class PlaceholderController {
|
||||
|
||||
Reference in New Issue
Block a user