Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically terminate all integration tests if `Failed to load ApplicationContext` error occurs

We have some integration tests(written using spring framework) that are failing due to bean initialisation exception which eventually leads to Failed to load ApplicationContext . As per my understanding from spring testing docs , the loading of ApplicationContext happens at class level, so my doubt is -

  1. Once the ApplicationContext fails (i.e. Failed to load ApplicationContext) due to bean initialisation exception during integration test class run, does the ApplicationContext try to spin up again(will be failing eventually) for each individual integration tests present in that particular integration test class ?

    1. Asking above scenario because we are seeing huge spike in number of connections to postgres when bean failure happens, it seems like for every integration test present(which eventually fails due to Failed to load ApplicationContext) in the integration test class, spring tries to create a new connection to postgres and doesn't destroy it before ApplicationContext failure. How can we stop this, please help with some suggestions.
  2. Also, once we get Failed to load ApplicationContext, is there a way to programmatically terminate the run of all the integration tests completely automatically ? If yes, please help how to achieve it ? Thanks.

Testing framework - junit + Spring

Update: Mentioned testing framework used.

like image 708
do5 Avatar asked Sep 05 '25 14:09

do5


1 Answers

UPDATE

I implemented built-in support for a "test context failure threshold" in Spring Framework 6.1 M1.

As of Spring Framework 6.1, a context failure threshold policy is in place which helps avoid repeated attempts to load a failing ApplicationContext. By default, the failure threshold is set to 1 which means that only one attempt will be made to load an ApplicationContext for a given context cache key. Any subsequent attempt to load the ApplicationContext for the same context cache key will result in an immediate IllegalStateException with an error message which explains that the attempt was preemptively skipped.

For further details, consult the Spring Framework reference manual.


Original Answer:

There is currently no way to abort integration tests if the ApplicationContext repeatedly fails to load.

To vote for such support, please see this Spring Framework issue.

like image 122
Sam Brannen Avatar answered Sep 09 '25 13:09

Sam Brannen