Reload Spring application context after every test

hudi :

I have a test Class which contains 2 test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)

public class MyITest extends implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Test
    public void test1() throws Exception {}

    @Test
    public void test2() throws Exception {}        
}

When I run tests individually I get no errors, but when I run all tests together there is a failure. This failure is due to some tests modifying the application context:

  b = beanFactory.getBean("logDataSource", BasicDataSource.class);
  b.set ...

Is there an option to run this test separately? I just want when test1 start it read all necessary things then run test and then close all necessary things. And then start test2.

groodt :

You can use the @DirtiesContext annotation on the test class that modifies the application context.

Java Doc

Spring documentation

By default, this will mark the application context as dirty after the entire test class is run. If you would like to mark the context as dirty after a single test method, then you can either annotate the test method instead or set the classMode property to AFTER_EACH_TEST_METHOD at your class level annotation.

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reload or refresh a Spring application context inside a test method?

Spring boot test: context loaded for every test?

Start and shutdown Spring application context in a unit test

Spring JUnit Test not loading full Application Context

Spring JUnit Test not loading full Application Context

Spring integration test with context hierarchy child context bean as application listener

Squish close my Application after every Test Case

How to exclude spring boot application beans from the test App context?

JUnit + Derby + Spring: drop in-memory db after every test

Loading context after application context

How to execute jobs just after spring loads application context?

Ajax post request after every reload of page

Reload the bootstrap datatable after every minute?

Getting Spring Application Context

Spring application context encryption

Spring application context is null

Spring Application context not found

Spring Application - Application Context IOC

Can Spring Boot test classes reuse application context for faster test run?

Is it possble to reset Spring-Test's context cache before a test suite runs (not after)

Is it safe to clear the MDC context after every HTTP request with Spring Cloud Sleuth?

Perform a task in spring after application context start so that task failure wont affect application start

How to instantiate groovy.sql.Sql with parameters from spring datasource setup from test application.yml in a SpringBoot application test context?

Spring test : Missing servlet context

Test method mockito with spring context

Unit test context configuration spring

Spring @Configuration not overriden in test context

Servlet context attribute from Spring application context

How to integrate spring context of library to application context?