Skip to content

Commit dac7806

Browse files
committed
Test beforeTestMethod delays creation of SecurityContext
Issue gh-6591
1 parent 97ee6d6 commit dac7806

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListenerTests.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public void beforeTestMethodWhenWithMockUserTestExecutionThenTestContextSupplier
124124
assertThat(supplierCaptor.getValue().get().getAuthentication()).isNotNull();
125125
}
126126

127+
@Test
128+
// gh-6591
129+
public void beforeTestMethodWhenTestExecutionThenDelayFactoryCreate() throws Exception {
130+
Method testMethod = TheTest.class.getMethod("withUserDetails");
131+
when(this.testContext.getApplicationContext()).thenReturn(this.applicationContext);
132+
// do not set a UserDetailsService Bean so it would fail if looked up
133+
when(this.testContext.getTestMethod()).thenReturn(testMethod);
134+
135+
this.listener.beforeTestMethod(this.testContext);
136+
// bean lookup of UserDetailsService would fail if it has already been looked up
137+
}
138+
127139
@Test
128140
public void beforeTestExecutionWhenTestContextNullThenSecurityContextNotSet() {
129141
this.listener.beforeTestExecution(this.testContext);
@@ -158,7 +170,10 @@ public void withMockUserTestMethod() {
158170
@WithMockUser
159171
public void withMockUserDefault() {
160172
}
161-
}
162173

174+
@WithUserDetails(setupBefore = TestExecutionEvent.TEST_EXECUTION)
175+
public void withUserDetails() {
176+
}
177+
}
163178

164179
}

0 commit comments

Comments
 (0)