Skip to content

Commit b18a7c1

Browse files
committed
SAML2 autoconfiguration is not imported by @WebMvcTest
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 21ec76c commit b18a7c1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,18 @@ dependencies {
114114
testImplementation("org.junit.platform:junit-platform-launcher")
115115
testImplementation("org.mockito:mockito-core")
116116
testImplementation("org.mockito:mockito-junit-jupiter")
117+
testImplementation("org.opensaml:opensaml-core:4.0.1")
118+
testImplementation("org.opensaml:opensaml-saml-api:4.0.1")
119+
testImplementation("org.opensaml:opensaml-saml-impl:4.0.1")
117120
testImplementation("org.skyscreamer:jsonassert")
118121
testImplementation("org.springframework:spring-core-test")
119122
testImplementation("org.springframework.hateoas:spring-hateoas")
120123
testImplementation("org.springframework.plugin:spring-plugin-core")
124+
testImplementation("org.springframework.security:spring-security-saml2-service-provider") {
125+
exclude group: "org.opensaml", module: "opensaml-core"
126+
exclude group: "org.opensaml", module: "opensaml-saml-api"
127+
exclude group: "org.opensaml", module: "opensaml-saml-impl"
128+
}
121129
testImplementation("org.thymeleaf:thymeleaf")
122130
}
123131

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc.imports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConf
44
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration
55
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration
66
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration
7+
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration
78
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
89
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration
910
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
25+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
29+
/**
30+
* Tests for {@link WebMvcTest @WebMvcTest} with SAML2.
31+
*
32+
* @author Dmytro Nosan
33+
*/
34+
@WebMvcTest(controllers = ExampleController1.class, properties = {
35+
"spring.security.saml2.relyingparty.registration.test.entity-id=relyingparty",
36+
"spring.security.saml2.relyingparty.registration.test.assertingparty.entity-id=assertingparty",
37+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.url=https://example.com",
38+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.sign-request=false" })
39+
class WebMvcTestSaml2Tests {
40+
41+
@Autowired
42+
private MockMvc mockMvc;
43+
44+
@Test
45+
void shouldRedirectToLogin() throws Exception {
46+
this.mockMvc.perform(get("/one"))
47+
.andExpect(status().isFound())
48+
.andExpect(redirectedUrlPattern("**/saml2/authenticate?registrationId=test"));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)