我有服务器@SpringBootTest
个测试,带有一个带有data.sql
且带有测试数据的简化数据库(h2)。每个测试都有其自己的域,并且随着data.sql
的增长。我想知道是否有任何针对每个@SpringBootTest
注释包括特定data.sql的选项,例如de properties source。
我知道有一个选项,即创建不同的application.properties
并在其中指定要加载的data.sql
,但我想知道是否有更干净的选项。
目前我有这个:
@SpringBootTest(classes = {MyApplication.class}, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MyFunctionalTest {
我想要这样的东西:
@SpringBootTest(classes = {MyApplication.class}, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@H2Data("sepecific.sql)
public class MyFunctionalTest {
答案 0 :(得分:0)
您好,我认为我可能已经在常规的Spring Boot应用程序中完成了此操作,但尚未在Test上尝试过,但几乎可以确定它可以在Test上运行。您需要像本文中一样声明多个DataSourceInitializers
:
@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;
}