Skip to content

Commit 4e1f97a

Browse files
committed
Document WithSecurityContextTestExecutionListener Nested Support
Issue gh-9179
1 parent 87d8741 commit 4e1f97a

File tree

1 file changed

+22
-0
lines changed
  • docs/manual/src/docs/asciidoc/_includes/servlet/test

1 file changed

+22
-0
lines changed

Diff for: docs/manual/src/docs/asciidoc/_includes/servlet/test/method.adoc

+22
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ For example, the following would run every test with a user with the username "a
135135
public class WithMockUserTests {
136136
----
137137

138+
If you are using JUnit 5's `@Nested` test support, you can also place the annotation on the enclosing class to apply to all nested classes.
139+
For example, the following would run every test with a user with the username "admin", the password "password", and the roles "ROLE_USER" and "ROLE_ADMIN" for both test methods.
140+
141+
[source,java]
142+
----
143+
@ExtendWith(SpringExtension.class)
144+
@ContextConfiguration
145+
@WithMockUser(username="admin",roles={"USER","ADMIN"})
146+
public class WithMockUserTests {
147+
148+
@Nested
149+
public class TestSuite1 {
150+
// ... all test methods use admin user
151+
}
152+
153+
@Nested
154+
public class TestSuite2 {
155+
// ... all test methods use admin user
156+
}
157+
}
158+
----
159+
138160
By default the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
139161
This is the equivalent of happening before JUnit's `@Before`.
140162
You can change this to happen during the `TestExecutionListener.beforeTestExecution` event which is after JUnit's `@Before` but before the test method is invoked.

0 commit comments

Comments
 (0)