3

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.

do5
  • 51
  • 5
  • FYI: I updated my original answer. Spring Framework 6.1 will provide built-in support for a "test context failure threshold". – Sam Brannen Aug 25 '23 at 09:08

3 Answers3

1

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.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
0
  1. If you have multiple integration tests in test class then it will fail multiple times.

  2. you can terminate all the integration test cases using the dependsOnGroups property.

For example, you can have a method to check whether there is a bean exception failure or not and declare all other tests as dependent on this method. But this can be done using TestNG

@Test(groups = { "isApplicationContextFailed" }) // you can run with simply @Test here if there is only one independent test.
public void isApplicationContextFailed() {}

@Test(dependsOnMethods = { "isApplicationContextFailed" })
public void queryTestOne() {}

@Test(dependsOnMethods = { "isApplicationContextFailed" })
public void queryTestTwo() {}

you can explore more from TestNG -> https://testng.org/doc/documentation-main.html#dependent-methods

Note:- if you are using the JUnit there are some other ways you can do the same. Please update in that case.

  • Hi Narasimha, I have updated the question, mentioned junit + spring as testing framework. Please suggest. Thanks. – do5 Oct 28 '21 at 12:05
  • Then you can simply use the **@BeforeClass** annotation in a dependent method that will firstly check for the application context failure. – NARASIMHA RAO NANDIKONDA Oct 28 '21 at 12:11
0

If you are using JUnit then add below login in your test classes it will work.

public class BaseClass {

@BeforeClass
    public void isApplicationContextFailed() {
        //logic to check for application failure
    }


//continue with your test methods.
}

To suggest you through below references to get in detail.

https://junit.org/junit4/javadoc/4.13/org/junit/BeforeClass.html

https://howtodoinjava.com/testng/testng-before-and-after-annotations/