-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Clarifying documentation on including a top-level @TestConfiguration class in a test #30513
Comments
Thanks for providing the reproducer project! Our documentation doesn't mention that
I agree that it's strange that the behavior changes when using I'll flag this as team attention, let's see what the rest of the team thinks. |
As far as I know, Spring Framework's testing support doesn't process Instead of
I think we need to correct the documentation as this section shows
To me, this is saying that a single top-level class annotated with |
Yep, you're right Andy. My "workaround" with the |
@wilkinsona @mhalbritter Thank you for your research, but I can not agree. I updated my example code to proove this. |
Apologies, @kesslerj. I'd forgotten about Boot's The problem you're seeing is due to ordering and the different times at which beans are defined between component scanning, As I said above, you can use |
@wilkinsona thank you for your detailed answer. Unfortunately none of the solutions is really satisfying our needs. Let me quickly explain our desired setup: Our previous approach was to use I only see two other approaches:
I extended my example repository and added a setup with an abstract test class, see branch https://github.com/spring-boot-demos/demo-testing-bean-overriding/tree/abstract-test-class. Do you have any other ideas how we can go on with our abstract test class in a comfortable way? Is it somehow possible to not redefine all used configurations with |
A test configuration annotated with
@TestConfiguration
does not override a bean, if it is defined as a separate class. Only using@TesConfiguration
on an inner class works in this case.Not working:
Working:
My example uses spring boot 2.6.6, code can be found here: https://github.com/spring-boot-demos/demo-testing-bean-overriding
I read several blogs and articles and found some other examples, which do not work, e.g. this blog with example code which is very similar to my example code.
In my understanding, the behaviour of a class annotated with
@TestConfiguration
should be the same, no matter if defined as inner or separate class.The text was updated successfully, but these errors were encountered: