Fix tests using root cause for exception messages

Closes gh-11372
This commit is contained in:
Steve Riesenberg
2022-06-14 15:42:43 -05:00
parent d98dab5917
commit c7df39a3e6
12 changed files with 30 additions and 18 deletions

View File

@@ -98,7 +98,7 @@ public class SecurityNamespaceHandlerTests {
expectClassUtilsForNameThrowsNoClassDefFoundError(className);
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
.withMessageContaining("NoClassDefFoundError: " + className);
.havingRootCause().isInstanceOf(NoClassDefFoundError.class).withMessage(className);
}
@Test
@@ -115,7 +115,7 @@ public class SecurityNamespaceHandlerTests {
expectClassUtilsForNameThrowsClassNotFoundException(className);
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
.withMessageContaining("ClassNotFoundException: " + className);
.havingRootCause().isInstanceOf(ClassNotFoundException.class).withMessage(className);
}
@Test

View File

@@ -152,7 +152,7 @@ public class WebSecurityConfigurationTests {
@Test
public void loadConfigWhenWebSecurityConfigurersHaveSameOrderThenThrowBeanCreationException() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring.register(DuplicateOrderConfig.class).autowire())
.isThrownBy(() -> this.spring.register(DuplicateOrderConfig.class).autowire()).havingRootCause()
.withMessageContaining("@Order on WebSecurityConfigurers must be unique")
.withMessageContaining(DuplicateOrderConfig.WebConfigurer1.class.getName())
.withMessageContaining(DuplicateOrderConfig.WebConfigurer2.class.getName());

View File

@@ -56,8 +56,14 @@ public class AccessDeniedConfigTests {
@Test
public void configureWhenAccessDeniedHandlerIsMissingLeadingSlashThenException() {
SpringTestContext context = this.spring.configLocations(this.xml("NoLeadingSlash"));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.autowire())
.withMessageContaining("errorPage must begin with '/'");
/*
* NOTE: Original error message "errorPage must begin with '/'" no longer shows up
* in stack trace as of Spring Framework 6.x.
*
* See https://github.com/spring-projects/spring-framework/issues/25162.
*/
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.autowire()).havingRootCause()
.withMessageContaining("Property 'errorPage' threw exception");
}
@Test

View File

@@ -61,7 +61,7 @@ public class HttpCorsConfigTests {
@Test
public void autowireWhenMissingMvcThenGivesInformativeError() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring.configLocations(this.xml("RequiresMvc")).autowire())
.isThrownBy(() -> this.spring.configLocations(this.xml("RequiresMvc")).autowire()).havingRootCause()
.withMessageContaining(
"Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext");
}

View File

@@ -386,10 +386,16 @@ public class HttpHeadersConfigTests {
@Test
public void configureWhenXssProtectionDisabledAndBlockSetThenAutowireFails() {
/*
* NOTE: Original error message "Cannot set block to true with enabled false" no
* longer shows up in stack trace as of Spring Framework 6.x.
*
* See https://github.com/spring-projects/spring-framework/issues/25162.
*/
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring
.configLocations(this.xml("DefaultsDisabledWithXssProtectionDisabledAndBlockSet")).autowire())
.withMessageContaining("Cannot set block to true with enabled false");
.havingRootCause().withMessageContaining("Property 'block' threw exception");
}
@Test
@@ -445,14 +451,14 @@ public class HttpHeadersConfigTests {
public void configureWhenUsingHpkpWithoutPinsThenAutowireFails() {
assertThatExceptionOfType(XmlBeanDefinitionStoreException.class)
.isThrownBy(() -> this.spring.configLocations(this.xml("DefaultsDisabledWithEmptyHpkp")).autowire())
.withMessageContaining("The content of element 'hpkp' is not complete");
.havingRootCause().withMessageContaining("The content of element 'hpkp' is not complete");
}
@Test
public void configureWhenUsingHpkpWithEmptyPinsThenAutowireFails() {
assertThatExceptionOfType(XmlBeanDefinitionStoreException.class)
.isThrownBy(() -> this.spring.configLocations(this.xml("DefaultsDisabledWithEmptyPins")).autowire())
.withMessageContaining("The content of element 'pins' is not complete");
.havingRootCause().withMessageContaining("The content of element 'pins' is not complete");
}
@Test