-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
JUnit Jupiter @Nested class cannot share enclosing class's ApplicationContext if nested class is deemed to be a configuration candidate [SPR-16595] #21136
Comments
Sam Brannen commented This is related to #19930. |
Sam Brannen commented
Well, that doesn't appear to be the case in Core Spring. For example, annotating Thus, I assume it is Spring Boot Test's custom support for annotating test classes with |
Sam Brannen commented Oops. I misread your explanation. In any case, annotating |
Sam Brannen commented A little digging reveals that So perhaps Spring Boot should look into a solution? |
Sam Brannen commented Or... Juergen Hoeller, do you think Core Spring should do anything here? |
Juergen Hoeller commented Stéphane Nicoll, what's your take on this? |
Andy Wilkinson commented
That doesn't match what I'm seeing. Boot's |
Sam Brannen commented Oh. OK... I didn't literally debug it. So I'll have to investigate further. Thanks for the tip! |
Sam Brannen commented Aha.... I'm guessing you're referring to
So, it looks like a possible solution to the problem would be to add a "not an inner class" check. Juergen Hoeller, what do you think about that proposed change? |
Sam Brannen commented Actually, I think it would be a good idea to ensure that Or am I missing something? |
Juergen Hoeller commented As far as I see, at the |
Sam Brannen commented I've put some more thought into the overall issue, and I believe that the fix needs to be in Spring Boot. I unfortunately don't have time to go into details now, but I'll try to put together a list of failing use cases in Spring Boot Test with regard to how it currently supports |
Andy Wilkinson commented Thanks, Stefan. #19930 is tracking that part of the problem. |
Stefan Ludwig commented Ups - I moved the comment there. |
Sam Brannen commented
Juergen Hoeller, after having put more thought into it, I tend to agree with you. As I currently see it, I think there are only two real options.
Juergen Hoeller, Andy Wilkinson, Stéphane Nicoll, What do you think? Thoughts? |
Sam Brannen commented I'm actually not yet certain that proposal #1 above would work. Consider the fact that one can introduce multiple levels of Would that work in conjunction with how Spring Boot Test supports If you know the answer, feel free to share it. Otherwise just consider these rhetorical questions that I will investigate later. ;-) |
Juergen Hoeller commented This should be sorted out along with #21379 now since we're only going to consider nested classes on component types now, not for classes with any configuration indicators such as |
Sam Brannen commented Great, Juergen Hoeller! Thanks |
Sam Brannen commented FYI: I confirmed the fix by submitting a sample application. https://github.com/spring-projects/spring-framework-issues/tree/master/SPR-16595 Specifically, if you run the build against Spring |
Andy Wilkinson opened SPR-16595 and commented
This feels like a bug, although it may just be a (rather large) limitation.
It appears to be impossible for an inner test class annotated with
@Nested
to use the enclosing class's application context if the enclosing class is deemed to be a configuration candidate (for example, because it uses@Import
).To be able to share the context, the inner test class must have the same configuration as its enclosing class so that they have the same context cache key. In other words, if the enclosing class has used
@Import
the inner class must do so too. This leads to code like this:This fails to launch with the following exception:
An attempt is being made to create a
NestedTests
bean as its@Import
annotation means thatConfigurationClassUtils
considers it to be a configuration candidate. An instance of the enclosing class is needed to createNestedTests
as the class is not static. It fails because the enclosing class isn't available as a bean.Declaring the inner class as
static
overcomes the immediate problem but creates another. Being static prevents the inner class from accessing the enclosing class's non-static fields and declaring the enclosing class's fields as static means that they're no longer autowired.The situation that's described above means that many of Spring Boot's testing annotations do not work. In fact, as far as I can tell, the only one that does work is
@SpringBootTest
as it is not meta-annotated with@Import
.Affects: 5.0.4
Reference URL: spring-projects/spring-restdocs#490
Issue Links:
@Bean
introspection between@Configuration
classes and 'lite' beansReferenced from: commits spring-attic/spring-framework-issues@7a3db4a
1 votes, 6 watchers
The text was updated successfully, but these errors were encountered: