Skip to content

Commit 95a4dd1

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

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-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,53 @@
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.security.test.context.support.WithAnonymousUser;
24+
import org.springframework.test.web.servlet.MockMvc;
25+
26+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
30+
/**
31+
* Tests for {@link WebMvcTest @WebMvcTest} with SAML2.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
@WebMvcTest(controllers = ExampleController1.class, properties = {
36+
"spring.security.saml2.relyingparty.registration.test.entity-id=relyingparty",
37+
"spring.security.saml2.relyingparty.registration.test.assertingparty.entity-id=assertingparty",
38+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.url=https://example.com",
39+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.sign-request=false" })
40+
class WebMvcTestSaml2Tests {
41+
42+
@Autowired
43+
private MockMvc mockMvc;
44+
45+
@Test
46+
@WithAnonymousUser
47+
void loginShouldBeAuthorized() throws Exception {
48+
this.mockMvc.perform(get("/one"))
49+
.andExpect(status().isFound())
50+
.andExpect(redirectedUrlPattern("**/saml2/authenticate?registrationId=test"));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)