I have serveral @SpringBootTest
tests,with an enbebbed database(h2) with a data.sql
with the data for the tests. Each test has it's own domain and as the data.sql
is growing large. I would like to know if there is any option to include a specific data.sql for each @SpringBootTest
annotation, like de properties source.
I know there is a option, which is, create different application.properties
and specify there the data.sql
to load, but I would like to know if there are cleaner options.
Currently I have this:
@SpringBootTest(classes = {MyApplication.class}, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MyFunctionalTest {
And I would like something like this:
@SpringBootTest(classes = {MyApplication.class}, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@H2Data("sepecific.sql)
public class MyFunctionalTest {
Hi in my opinion it is possible I have done it for regular spring boot application , I ave not tried it for Test but I am almost sure that it will work for test. You need to declare multiple DataSourceInitializers
like I have done in this post:
@Bean
public DataSourceInitializer dataSourceInitializer1(@Qualifier("datasource1") DataSource datasource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h22.sql"));
resourceDatabasePopulator.addScript(new ClassPathResource("data-h22.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(datasource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
@Bean
public DataSourceInitializer dataSourceInitializer2(@Qualifier("datasource2") DataSource datasource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h21.sql"));
resourceDatabasePopulator.addScript(new ClassPathResource("data-h21.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(datasource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
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.