WithSecurityContextTestExecutionListener Supports Nested Classes

WithSecurityContextTestExecutionListener now supports nested classes. If
the class is nested WithSecurityContext is not found, then the enclosing
class is looked at until there is no enclosing class.

Closes gh-9179
This commit is contained in:
Rob Winch
2020-11-03 14:06:46 -06:00
parent d0d655e18d
commit 87d8741730
2 changed files with 60 additions and 9 deletions

View File

@@ -92,6 +92,30 @@ public class WithSecurityContextTestExcecutionListenerTests {
assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("user");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void beforeTestMethodInnerClass() throws Exception {
Class testClass = OuterClass.InnerClass.class;
Method testNoAnnotation = ReflectionUtils.findMethod(testClass, "testNoAnnotation");
given(this.testContext.getTestClass()).willReturn(testClass);
given(this.testContext.getTestMethod()).willReturn(testNoAnnotation);
given(this.testContext.getApplicationContext()).willThrow(new IllegalStateException(""));
this.listener.beforeTestMethod(this.testContext);
assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("user");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void beforeTestMethodInnerInnerClass() throws Exception {
Class testClass = OuterClass.InnerClass.InnerInnerClass.class;
Method testNoAnnotation = ReflectionUtils.findMethod(testClass, "testNoAnnotation");
given(this.testContext.getTestClass()).willReturn(testClass);
given(this.testContext.getTestMethod()).willReturn(testNoAnnotation);
given(this.testContext.getApplicationContext()).willThrow(new IllegalStateException(""));
this.listener.beforeTestMethod(this.testContext);
assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("user");
}
// gh-3962
@Test
public void withSecurityContextAfterSqlScripts() {
@@ -166,4 +190,23 @@ public class WithSecurityContextTestExcecutionListenerTests {
}
@WithMockUser
static class OuterClass {
static class InnerClass {
void testNoAnnotation() {
}
static class InnerInnerClass {
void testNoAnnotation() {
}
}
}
}
}