-
Notifications
You must be signed in to change notification settings - Fork 40.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
Application Context initialized twice during test when exception thrown during initialization #24888
Comments
@QwertGold I think I understand the description, but it would be useful if you could provide a sample application that shows the problem. |
Cool, let me build a small project to illustrate |
I'm unable to reproduce this in a simple stand alone project. |
I figure out how to reproduce this, it seems to happen when the test has a real Web server environment I started with JUnit 4 and boot 2.3.7 as this is what we use, but the behavior is the same for Junit5 and boot 2.4.2, the repo has commits for both cases, https://github.com/QwertGold/spring-24888 |
hit same issue , any update ? I suggest adding below in the catch block{}
Or
|
Thanks for the sample and for your patience while we found time to start looking at this, @QwertGold. The problem doesn't occur with a mock web environment (plain When there's a full-blown web environment, |
The behaviour's the same back in 1.4.0-M2 when the printing of the condition evaluation report was first introduced (see #4901 and e5f2241) and in 1.4.1 when it was refined (see #6874 and 7134586). When refresh fails the context won't be available so I can't see how the current approach will be able to access the context to generate the report. We could just stop using Flagging for an upcoming team meeting so that we can discuss what to do. |
This comment has been minimized.
This comment has been minimized.
Just pulled my hair out trying to figure out why my context was loading twice as I was trying to figure out what was causing it to have an error during initialization in the first place.....not fun! |
I'm favor of getting rid of What do you think about introducing an SPI (in Spring Framework 6.0) in the TestContext framework for "processing" Related Issues: |
That sounds great, Sam. Thanks. We'll be left with the problem described in this issue in 2.x, but with a much better path forward in 3.0. |
|
I noticed that when I have a Spring Boot test, and the context fails to initialize, the Banner is printed twice, and I decided to look into why.
When an exception is thrown during initialization,
outputConditionEvaluationReport(testContext)
is called, which eventually leads to the context being initialized a second time insideDefaultCacheAwareContextLoaderDelegate::loadContext
.This is more a nuisance than a bug, as it only happens in test code; the cost is 'just' developer time and build resources.
I this particular case I want to fail fast, so I throw an exception because I know bad things will happen later.
I'm trying to safeguard against less experienced developers, using resources outside the intended lifecycle. During tests we automatically create external resources in an early lifecycle phase, these resources may only be accessed during later phases, and after the application is started. If a developer violate this rule, and tries to access a resource during bean construction or in an earlier lifecycle, I can detect it, and throw an IlligealStateException. If I don't do this I will get an exception later from the resource Api, and it can be hard for junior developers to identify that they have violated the resource lifecycle, if they get a 404/500 error from a HttpClient.
One possible solution, is to have an
AbortTestContextInitializationException
, that you could throw, if you decide that theTestContext
is in a state where it does not make sense to continue executing, and there is no point in trying to generate theConditionEvaluationReport
. Ideally the failure of the initialization would be cached, so other tests using the same context would fail immediately instead of using build resources , trying to create the context twice for every test.The text was updated successfully, but these errors were encountered: