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

Viewed 386

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.

3 Answers

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.

  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.

Related