Skip to content

Commit c7df39a

Browse files
author
Steve Riesenberg
committed
Fix tests using root cause for exception messages
Closes gh-11372
1 parent d98dab5 commit c7df39a

File tree

12 files changed

+30
-18
lines changed

12 files changed

+30
-18
lines changed

config/src/integration-test/java/org/springframework/security/config/ldap/EmbeddedLdapServerContextSourceFactoryBeanITests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void contextSourceFactoryBeanWhenCustomManagerDnThenAuthenticates() throw
7777
public void contextSourceFactoryBeanWhenManagerDnAndNoPasswordThenException() {
7878
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
7979
.isThrownBy(() -> this.spring.register(CustomManagerDnNoPasswordConfig.class).autowire())
80-
.withRootCauseInstanceOf(IllegalStateException.class)
80+
.havingRootCause().isInstanceOf(IllegalStateException.class)
8181
.withMessageContaining("managerPassword is required if managerDn is supplied");
8282
}
8383

config/src/test/java/org/springframework/security/config/SecurityNamespaceHandlerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void filterNoClassDefFoundError() throws Exception {
9898
expectClassUtilsForNameThrowsNoClassDefFoundError(className);
9999
assertThatExceptionOfType(BeanDefinitionParsingException.class)
100100
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
101-
.withMessageContaining("NoClassDefFoundError: " + className);
101+
.havingRootCause().isInstanceOf(NoClassDefFoundError.class).withMessage(className);
102102
}
103103

104104
@Test
@@ -115,7 +115,7 @@ public void filterChainProxyClassNotFoundException() throws Exception {
115115
expectClassUtilsForNameThrowsClassNotFoundException(className);
116116
assertThatExceptionOfType(BeanDefinitionParsingException.class)
117117
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
118-
.withMessageContaining("ClassNotFoundException: " + className);
118+
.havingRootCause().isInstanceOf(ClassNotFoundException.class).withMessage(className);
119119
}
120120

121121
@Test

config/src/test/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void loadConfigWhenSecurityFilterChainsHaveOrderOnBeanDefinitionsThenFilt
152152
@Test
153153
public void loadConfigWhenWebSecurityConfigurersHaveSameOrderThenThrowBeanCreationException() {
154154
assertThatExceptionOfType(BeanCreationException.class)
155-
.isThrownBy(() -> this.spring.register(DuplicateOrderConfig.class).autowire())
155+
.isThrownBy(() -> this.spring.register(DuplicateOrderConfig.class).autowire()).havingRootCause()
156156
.withMessageContaining("@Order on WebSecurityConfigurers must be unique")
157157
.withMessageContaining(DuplicateOrderConfig.WebConfigurer1.class.getName())
158158
.withMessageContaining(DuplicateOrderConfig.WebConfigurer2.class.getName());

config/src/test/java/org/springframework/security/config/http/AccessDeniedConfigTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ public class AccessDeniedConfigTests {
5656
@Test
5757
public void configureWhenAccessDeniedHandlerIsMissingLeadingSlashThenException() {
5858
SpringTestContext context = this.spring.configLocations(this.xml("NoLeadingSlash"));
59-
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.autowire())
60-
.withMessageContaining("errorPage must begin with '/'");
59+
/*
60+
* NOTE: Original error message "errorPage must begin with '/'" no longer shows up
61+
* in stack trace as of Spring Framework 6.x.
62+
*
63+
* See https://github.com/spring-projects/spring-framework/issues/25162.
64+
*/
65+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.autowire()).havingRootCause()
66+
.withMessageContaining("Property 'errorPage' threw exception");
6167
}
6268

6369
@Test

config/src/test/java/org/springframework/security/config/http/HttpCorsConfigTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class HttpCorsConfigTests {
6161
@Test
6262
public void autowireWhenMissingMvcThenGivesInformativeError() {
6363
assertThatExceptionOfType(BeanCreationException.class)
64-
.isThrownBy(() -> this.spring.configLocations(this.xml("RequiresMvc")).autowire())
64+
.isThrownBy(() -> this.spring.configLocations(this.xml("RequiresMvc")).autowire()).havingRootCause()
6565
.withMessageContaining(
6666
"Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext");
6767
}

config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,16 @@ public void requestWhenDisablingXssProtectionThenDefaultsToZero() throws Excepti
386386

387387
@Test
388388
public void configureWhenXssProtectionDisabledAndBlockSetThenAutowireFails() {
389+
/*
390+
* NOTE: Original error message "Cannot set block to true with enabled false" no
391+
* longer shows up in stack trace as of Spring Framework 6.x.
392+
*
393+
* See https://github.com/spring-projects/spring-framework/issues/25162.
394+
*/
389395
assertThatExceptionOfType(BeanCreationException.class)
390396
.isThrownBy(() -> this.spring
391397
.configLocations(this.xml("DefaultsDisabledWithXssProtectionDisabledAndBlockSet")).autowire())
392-
.withMessageContaining("Cannot set block to true with enabled false");
398+
.havingRootCause().withMessageContaining("Property 'block' threw exception");
393399
}
394400

395401
@Test
@@ -445,14 +451,14 @@ public void insecureRequestWhenUsingCustomHstsRequestMatcherThenIncludesHstsHead
445451
public void configureWhenUsingHpkpWithoutPinsThenAutowireFails() {
446452
assertThatExceptionOfType(XmlBeanDefinitionStoreException.class)
447453
.isThrownBy(() -> this.spring.configLocations(this.xml("DefaultsDisabledWithEmptyHpkp")).autowire())
448-
.withMessageContaining("The content of element 'hpkp' is not complete");
454+
.havingRootCause().withMessageContaining("The content of element 'hpkp' is not complete");
449455
}
450456

451457
@Test
452458
public void configureWhenUsingHpkpWithEmptyPinsThenAutowireFails() {
453459
assertThatExceptionOfType(XmlBeanDefinitionStoreException.class)
454460
.isThrownBy(() -> this.spring.configLocations(this.xml("DefaultsDisabledWithEmptyPins")).autowire())
455-
.withMessageContaining("The content of element 'pins' is not complete");
461+
.havingRootCause().withMessageContaining("The content of element 'pins' is not complete");
456462
}
457463

458464
@Test

itest/ldap/embedded-ldap-none/src/integration-test/java/org/springframework/security/LdapServerBeanDefinitionParserTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void closeAppContext() {
4343
public void apacheDirectoryServerIsStartedByDefault() {
4444
assertThatExceptionOfType(BeanDefinitionStoreException.class)
4545
.isThrownBy(() -> this.context = new ClassPathXmlApplicationContext("applicationContext-security.xml"))
46-
.withMessageContaining("Embedded LDAP server is not provided");
46+
.havingRootCause().withMessageContaining("Embedded LDAP server is not provided");
4747
}
4848

4949
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultAuthorizationCodeTokenResponseClientTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAu
258258
.getTokenResponse(authorizationCodeGrantRequest(this.clientRegistration.build())))
259259
.withMessageContaining(
260260
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
261-
.withMessageContaining("tokenType cannot be null");
261+
.havingRootCause().withMessageContaining("tokenType cannot be null");
262262
}
263263

264264
@Test
@@ -274,7 +274,7 @@ public void getTokenResponseWhenSuccessResponseAndMissingTokenTypeParameterThenT
274274
.getTokenResponse(authorizationCodeGrantRequest(this.clientRegistration.build())))
275275
.withMessageContaining(
276276
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
277-
.withMessageContaining("tokenType cannot be null");
277+
.havingRootCause().withMessageContaining("tokenType cannot be null");
278278
}
279279

280280
@Test

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultClientCredentialsTokenResponseClientTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAu
269269
.isThrownBy(() -> this.tokenResponseClient.getTokenResponse(clientCredentialsGrantRequest))
270270
.withMessageContaining(
271271
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
272-
.withMessageContaining("tokenType cannot be null");
272+
.havingRootCause().withMessageContaining("tokenType cannot be null");
273273
}
274274

275275
@Test
@@ -282,7 +282,7 @@ public void getTokenResponseWhenSuccessResponseAndMissingTokenTypeParameterThenT
282282
.isThrownBy(() -> this.tokenResponseClient.getTokenResponse(clientCredentialsGrantRequest))
283283
.withMessageContaining(
284284
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
285-
.withMessageContaining("tokenType cannot be null");
285+
.havingRootCause().withMessageContaining("tokenType cannot be null");
286286
}
287287

288288
@Test

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultJwtBearerTokenResponseClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAu
182182
.isThrownBy(() -> this.tokenResponseClient.getTokenResponse(jwtBearerGrantRequest))
183183
.withMessageContaining(
184184
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
185-
.withMessageContaining("tokenType cannot be null");
185+
.havingRootCause().withMessageContaining("tokenType cannot be null");
186186
}
187187

188188
@Test

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultPasswordTokenResponseClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAu
245245
.isThrownBy(() -> this.tokenResponseClient.getTokenResponse(passwordGrantRequest))
246246
.withMessageContaining(
247247
"[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response")
248-
.withMessageContaining("tokenType cannot be null");
248+
.havingRootCause().withMessageContaining("tokenType cannot be null");
249249
}
250250

251251
@Test

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultRefreshTokenTokenResponseClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAu
246246
.isThrownBy(() -> this.tokenResponseClient.getTokenResponse(refreshTokenGrantRequest))
247247
.withMessageContaining("[invalid_token_response] An error occurred while attempting to "
248248
+ "retrieve the OAuth 2.0 Access Token Response")
249-
.withMessageContaining("tokenType cannot be null");
249+
.havingRootCause().withMessageContaining("tokenType cannot be null");
250250
}
251251

252252
@Test

0 commit comments

Comments
 (0)