Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithSecurityContextTestExecutionListener Should Support Nested Classes #9179

Closed
wilkinsona opened this issue Nov 2, 2020 · 3 comments
Closed
Assignees
Labels
in: test An issue in spring-security-test type: enhancement A general enhancement
Milestone

Comments

@wilkinsona
Copy link
Member

Expected Behavior

Spring Framework 5.3 has added support for discovering tested configuration on an enclosing class. This is useful when using JUnit Jupiter's @Nested as it allows test configuration to be declared once (on the enclosing class) and then reused (by all the nested classes).

@MockUser should participate in this behaviour so that it can be declared once on the enclosing class and used by all the nested classes.

Current Behavior

@MockUser on the enclosing class is ignored when running the nested tests and most be redeclared on each @Nested test class.

Context

This is part of providing consistent testing support across the portfolio. The current inconsistencies make it hard for users to know when they can declare configuration once and when it must be repeated.

/cc @sbrannen

@wilkinsona wilkinsona added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Nov 2, 2020
@wilkinsona wilkinsona changed the title Build on Framework's support the nested test configuration discovery and inherit WithMockUser from enclosing class Build on Framework's support for nested test configuration discovery and inherit WithMockUser from enclosing class Nov 2, 2020
@eleftherias eleftherias added in: test An issue in spring-security-test and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 3, 2020
@rwinch rwinch self-assigned this Nov 3, 2020
@rwinch rwinch changed the title Build on Framework's support for nested test configuration discovery and inherit WithMockUser from enclosing class WithSecurityContextTestExecutionListener Should Support Nested Classes Nov 3, 2020
@rwinch rwinch closed this as completed in 87d8741 Nov 3, 2020
@rwinch rwinch added this to the 5.5.0-M1 milestone Nov 3, 2020
@rwinch
Copy link
Member

rwinch commented Nov 3, 2020

The WithMockUser support needs to work a little differently than the nested support in test. The support within TestContextAnnotationUtils requires EnclosingConfiguration.INHERIT but Spring Security's test support does not require an ApplicationContext to be present. Instead, WithSecurityContext and the related annotations are used unless overridden by a more specific location.

@sbrannen
Copy link
Member

sbrannen commented Nov 4, 2020

The WithMockUser support needs to work a little differently than the nested support in test.

Why should it work differently than other test annotations in the Spring portfolio?

The support within TestContextAnnotationUtils requires EnclosingConfiguration.INHERIT but Spring Security's test support does not require an ApplicationContext to be present.

TestContextAnnotationUtils does not require that an ApplicationContext be present. Rather, TestContextAnnotationUtils provides support for looking up any class-level annotations that are used within tests while honoring the new @NestedTestConfiguration semantics.

Note that EnclosingConfiguration.INHERIT is the default behavior in Spring Framework 5.3, but it's configurable via the SpringProperties mechanism and @NestedTestConfiguration.

Instead, WithSecurityContext and the related annotations are used unless overridden by a more specific location.

In 87d8741, I see that you do not make use of TestContextAnnotationUtils. Can you please explain the rationale for that?

@rwinch
Copy link
Member

rwinch commented Nov 6, 2020

@sbrannen Thank you for your review. This turns out it was a just a series of misunderstandings on my part. I went created and resolved gh-9193 to address your concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test An issue in spring-security-test type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants