Skip to content

Commit a923ec8

Browse files
committed
Update servlet test method docs to use include-code
References gh-16226 Signed-off-by: Joe Kuhel <[email protected]>
1 parent 41f3131 commit a923ec8

35 files changed

+1768
-519
lines changed

docs/modules/ROOT/pages/servlet/test/method.adoc

Lines changed: 18 additions & 519 deletions
Large diffs are not rendered by default.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethod;
18+
19+
import org.springframework.security.access.prepost.PreAuthorize;
20+
import org.springframework.security.config.core.MessageService;
21+
import org.springframework.security.core.Authentication;
22+
import org.springframework.security.core.context.SecurityContextHolder;
23+
24+
/**
25+
* A message service for demonstrating test support for method-based security.
26+
*/
27+
// tag::authenticated[]
28+
public class HelloMessageService implements MessageService {
29+
30+
@Override
31+
@PreAuthorize("isAuthenticated()")
32+
public String getMessage() {
33+
Authentication authentication = SecurityContextHolder.getContext()
34+
.getAuthentication();
35+
return "Hello " + authentication;
36+
}
37+
38+
@Override
39+
@PreAuthorize("isAuthenticated()")
40+
public String getJsrMessage() {
41+
Authentication authentication = SecurityContextHolder.getContext()
42+
.getAuthentication();
43+
return "Hello JSR " + authentication;
44+
}
45+
}
46+
// end::authenticated[]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethod;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
27+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
28+
import org.springframework.security.config.core.MessageService;
29+
import org.springframework.security.core.authority.AuthorityUtils;
30+
import org.springframework.security.core.context.SecurityContextHolder;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit.jupiter.SpringExtension;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
@ExtendWith(SpringExtension.class)
37+
@ContextConfiguration
38+
class HelloServiceTests {
39+
40+
@Autowired
41+
MessageService messageService;
42+
43+
@BeforeEach
44+
void setup() {
45+
UsernamePasswordAuthenticationToken user = UsernamePasswordAuthenticationToken.authenticated("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
46+
SecurityContextHolder.getContext().setAuthentication(user);
47+
}
48+
49+
@Test
50+
void helloServiceTest() {
51+
assertThat(messageService.getMessage())
52+
.contains("user")
53+
.contains("ROLE_USER");
54+
}
55+
56+
@EnableMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
57+
@Configuration
58+
static class Config {
59+
60+
@Bean
61+
MessageService messageService() {
62+
return new HelloMessageService();
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodmetaannotations;
18+
19+
import org.springframework.security.test.context.support.WithMockUser;
20+
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
24+
// tag::snippet[]
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@WithMockUser(value="rob",roles={"USER","ADMIN"})
27+
public @interface WithMockAdmin { }
28+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodmetaannotations;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
25+
import org.springframework.security.config.core.MessageService;
26+
import org.springframework.security.docs.servlet.test.testmethod.HelloMessageService;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
@ExtendWith(SpringExtension.class)
33+
@ContextConfiguration
34+
public class WithMockAdminTests {
35+
36+
@Autowired
37+
MessageService messageService;
38+
39+
@Test
40+
@WithMockAdmin
41+
void getMessageWithMockUserAdminRoles() {
42+
String message = messageService.getMessage();
43+
assertThat(message)
44+
.contains("rob")
45+
.contains("ROLE_ADMIN")
46+
.contains("ROLE_USER");
47+
}
48+
49+
@EnableMethodSecurity
50+
@Configuration
51+
static class Config {
52+
53+
@Bean
54+
MessageService messageService() {
55+
return new HelloMessageService();
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodmetaannotations;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
25+
import org.springframework.security.config.core.MessageService;
26+
import org.springframework.security.docs.servlet.test.testmethod.HelloMessageService;
27+
import org.springframework.security.test.context.support.WithMockUser;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit.jupiter.SpringExtension;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
@ExtendWith(SpringExtension.class)
34+
@ContextConfiguration
35+
public class WithMockUserTests {
36+
37+
@Autowired
38+
MessageService messageService;
39+
40+
@Test
41+
// tag::snippet[]
42+
@WithMockUser(username = "admin", roles = {"USER", "ADMIN"})
43+
// end::snippet[]
44+
void getMessageWithMockUserAdminRoles() {
45+
String message = messageService.getMessage();
46+
assertThat(message)
47+
.contains("admin")
48+
.contains("ROLE_ADMIN")
49+
.contains("ROLE_USER");
50+
}
51+
52+
@EnableMethodSecurity
53+
@Configuration
54+
static class Config {
55+
56+
@Bean
57+
MessageService messageService() {
58+
return new HelloMessageService();
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodsetup;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
26+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
27+
import org.springframework.security.docs.servlet.test.testmethod.HelloMessageService;
28+
import org.springframework.security.config.core.MessageService;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit.jupiter.SpringExtension;
31+
32+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
33+
34+
@ExtendWith(SpringExtension.class)
35+
@ContextConfiguration
36+
class WithMockUserSampleTests {
37+
38+
@Autowired
39+
MessageService messageService;
40+
41+
// tag::snippet[]
42+
@Test
43+
void getMessageUnauthenticated() {
44+
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
45+
.isThrownBy(() -> messageService.getMessage());
46+
}
47+
// end::snippet[]
48+
49+
@EnableMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
50+
@Configuration
51+
static class Config {
52+
53+
@Bean
54+
MessageService messageService() {
55+
return new HelloMessageService();
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodsetup;
18+
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit.jupiter.SpringExtension;
23+
24+
// tag::setup[]
25+
@ExtendWith(SpringExtension.class) // <1>
26+
@ContextConfiguration // <2>
27+
class WithMockUserTests {
28+
// ...
29+
}
30+
// end::setup[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-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.security.docs.servlet.test.testmethodwithanonymoususer;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.springframework.security.test.context.support.WithAnonymousUser;
22+
import org.springframework.security.test.context.support.WithMockUser;
23+
import org.springframework.test.context.junit.jupiter.SpringExtension;
24+
25+
// tag::snippet[]
26+
@ExtendWith(SpringExtension.class)
27+
@WithMockUser
28+
class WithUserClassLevelAuthenticationTests {
29+
30+
@Test
31+
void withMockUser1() {
32+
}
33+
34+
@Test
35+
void withMockUser2() {
36+
}
37+
38+
@Test
39+
@WithAnonymousUser
40+
void anonymous() throws Exception {
41+
// override default to run as anonymous user
42+
}
43+
}
44+
// end::snippet[]

0 commit comments

Comments
 (0)