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

Comment From: rwinch

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.

Comment From: sbrannen

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 87d8741730e1a70e470c9affe25dabf9757f4d37, I see that you do not make use of TestContextAnnotationUtils. Can you please explain the rationale for that?

Comment From: rwinch

@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.