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

Time:10-29

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.

CodePudding user response:

  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.

CodePudding user response:

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/

  • Related