Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringJUnit4ClassRunner does not close the Application Context at the end of JUnit test case

Tags:

java

junit

spring

I'm using SpringJUnit4ClassRunner in my JUnit 4 tests like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
public class MyTest {
    @Autowired
    private ConfigurableApplicationContext context;
    @Test
    public void test1() {
    . . .
    }
    @Test
    public void test2() {
    . . .
    }
    . . .
}

However, at the end of this test case the application context is not closed. I would like to have the application context closed at the end of the test case (NOT at the end of each individual unit-test in the test case).

So far I could come up with this work-around:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
public class MyTest {
    @Autowired
    private ConfigurableApplicationContext context;
    private static ConfigurableApplicationContext lastContext;
    @After
    public void onTearDown() {
        lastContext = context;
    }
    @AfterClass
    public static void onClassTearDown() {
        lastContext.close();
    }
    @Test
    public void test1() {
    . . .
    }
    @Test
    public void test2() {
    . . .
    }
    . . .
}

Is there a better solution?

like image 924
rustyx Avatar asked Sep 21 '11 10:09

rustyx


People also ask

What does failed to load application context mean?

However, sometimes in this situation, we may encounter the application context loading error “Failed to load ApplicationContext.” This error appears in the test classes because the application context isn't loaded in the test context.

How can you create a shared application context in a JUnit integration test?

How can you create a shared application context in a JUnit integration test. Spring's integration testing support has the following primary goals: To manage Spring IoC container caching between tests. By default, once loaded, the configured ApplicationContext is reused for each test.

What is @ContextConfiguration in JUnit?

@ContextConfiguration loads an ApplicationContext for Spring integration test. @ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration . The @ContextConfiguration annotation can also load a component annotated with @Component , @Service , @Repository etc.

How can you access the application context in a Spring integration test?

By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired , @Resource , or @Inject .


2 Answers

You can add the @DirtiesContext(classMode=ClassMode.AFTER_CLASS) at the class level and the context will get closed once all the tests in the methods are done. You will get the same functionality as your current code.

like image 123
gkamal Avatar answered Sep 25 '22 02:09

gkamal


How are you running your tests?

Spring is not closing the context by default on test case close. Instead it install shutdown hook that is run when JVM exits. This obscure mechanism was introduces to allow test context caching, which is a good thing.

From my experience this works correctly when JUnit tests are run both from my IDE and from maven-surefire-plugin. Your solution is a bit of a hack and certainly should not be needed.

like image 31
Tomasz Nurkiewicz Avatar answered Sep 23 '22 02:09

Tomasz Nurkiewicz