简体   繁体   中英

Prevent Spring bean re-initialization for each JUnit test

I have integration tests based on JUnit that access DB. We also use Liquibase Spring bean to init database.

If I try running multiple tests in parallel each of them tries to initialize DB using Liquibase causing locks and eventually failures since only one instance of Liquibase can modify DB at a time.

The tests are configured as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@Sql({"/schema/insert-test-data.sql"})

How can I configure DB initialization (schema and data) that it will be done only once and not for each test?

This is just idea not testet, but if you are not executing scripts in parallel what about using ScriptUtils or ResourceDatabasePopulator iside @Before method with some switch.

@Before
public void init(){
    if (wasInitialized)
        return;

    new ResourceDatabasePopulator(new ClassPathResource("path/to/sql.sql")).execute(dataSource);
    wasInitialized = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM