Include `ErrorAttributes` in @WebMvcTest

Import the additional auto-configuration classes required for
`ErrorAttributes` and change the filter so that it's no longer
hidden.

Fixes gh-7867
This commit is contained in:
Phillip Webb 2017-01-04 15:25:33 -08:00
parent 803eddf6b2
commit e015e13b4d
3 changed files with 15 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
@ -54,6 +55,7 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
includes.add(FilterRegistrationBean.class);
includes.add(DelegatingFilterProxyRegistrationBean.class);
includes.add(HandlerMethodArgumentResolver.class);
includes.add(ErrorAttributes.class);
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
};

View File

@ -79,7 +79,9 @@ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
# DefaultTestExecutionListenersPostProcessors

View File

@ -24,9 +24,11 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.isA;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@ -48,6 +50,9 @@ public class WebMvcTestAllControllersIntegrationTests {
@Autowired
private MockMvc mvc;
@Autowired(required = false)
private ErrorAttributes errorAttributes;
@Test
public void shouldFindController1() throws Exception {
this.mvc.perform(get("/one")).andExpect(content().string("one"))
@ -78,4 +83,10 @@ public class WebMvcTestAllControllersIntegrationTests {
this.mvc.perform(get("/three/invalid"));
}
@Test
public void shouldNotFilterErrorAttributes() throws Exception {
assertThat(this.errorAttributes).isNotNull();
}
}