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:
parent
803eddf6b2
commit
e015e13b4d
|
|
@ -21,6 +21,7 @@ import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
|
||||||
import org.springframework.boot.context.TypeExcludeFilter;
|
import org.springframework.boot.context.TypeExcludeFilter;
|
||||||
import org.springframework.boot.jackson.JsonComponent;
|
import org.springframework.boot.jackson.JsonComponent;
|
||||||
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
|
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
|
||||||
|
|
@ -54,6 +55,7 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
|
||||||
includes.add(FilterRegistrationBean.class);
|
includes.add(FilterRegistrationBean.class);
|
||||||
includes.add(DelegatingFilterProxyRegistrationBean.class);
|
includes.add(DelegatingFilterProxyRegistrationBean.class);
|
||||||
includes.add(HandlerMethodArgumentResolver.class);
|
includes.add(HandlerMethodArgumentResolver.class);
|
||||||
|
includes.add(ErrorAttributes.class);
|
||||||
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
|
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,9 @@ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
|
||||||
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
||||||
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
||||||
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
|
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
|
||||||
|
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
|
||||||
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
|
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
|
||||||
|
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
|
||||||
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
|
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
|
||||||
|
|
||||||
# DefaultTestExecutionListenersPostProcessors
|
# DefaultTestExecutionListenersPostProcessors
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,11 @@ import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.CoreMatchers.isA;
|
import static org.hamcrest.CoreMatchers.isA;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
|
@ -48,6 +50,9 @@ public class WebMvcTestAllControllersIntegrationTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mvc;
|
private MockMvc mvc;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ErrorAttributes errorAttributes;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFindController1() throws Exception {
|
public void shouldFindController1() throws Exception {
|
||||||
this.mvc.perform(get("/one")).andExpect(content().string("one"))
|
this.mvc.perform(get("/one")).andExpect(content().string("one"))
|
||||||
|
|
@ -78,4 +83,10 @@ public class WebMvcTestAllControllersIntegrationTests {
|
||||||
this.mvc.perform(get("/three/invalid"));
|
this.mvc.perform(get("/three/invalid"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotFilterErrorAttributes() throws Exception {
|
||||||
|
assertThat(this.errorAttributes).isNotNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue