H2 DB 初始设置脚本在 JUNIT 中被多次调用
H2 DB Initial Set Up scripts are getting called multiple times in JUNIT
我正在尝试使用 H2 DB 为我的微服务编写 JUNIT 测试用例。我有一些 .sql 文件用于数据库的初始设置,比如创建模式、表等。我在这里看到的一个问题是 .sql 文件被多次调用,每个 JUNIT 一次测试文件。有什么办法可以处理这种情况吗?我的数据库设置文件如下所示 -
@Configuration
@ComponentScan(basePackages={"com.sample.repository"})
public class SampleDBConfig {
JdbcTemplate jdbcTemplate = null;
DataSource dataSource = null;
@Bean
public JdbcTemplate jdbcTemplate()
{
if(jdbcTemplate == null){
LOGGER.info("JdbcTemplate is null, so calling to create ");
jdbcTemplate = new JdbcTemplate(getDataSource());
}else{
LOGGER.info("JdbcTemplate is already set");
}
return jdbcTemplate;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
DataSource getDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
if(dataSource == null){
dataSource = builder
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:folder/initial-setup.sql")
.build();
}
return dataSource;
}
}
我的一个 JUNIT 测试 class 看起来像这样 -
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SampleDBConfig.class)
public class XyxImplTest{
@Autowired
ClassInTestImpl classInTestImpl ;
@Test
public void testMethod(){
.......
}
}
其中一个 class 测试是这样的 -
@Component
@Transactional(rollbackFor = Exception.class)
public class ClassInTestImpl implements ClassInTest {
@Autowired
private JdbcTemplate jdbcTemplate;
.....
我遇到了同样的问题。我所做的快速修复是,我 din 配置数据源 bean。在创建数据源的属性文件中添加了相关的 spring 属性。
而且我在第一次测试中使用了@Sql("classpath:data1.sql"),所以脚本运行只在那个测试套件中使用了一次。它对我有用。
我知道这是一个有点老的话题,但如果其他人有类似的问题,请添加一些注释。我遇到了同样的问题,当 H2 尝试为每个测试文件插入数据时,我遇到了唯一约束违规错误。
最后是最新的H2版本导致的。如果将 H2 版本从 1.4.200 更改为 1.4.199,效果会很好。无需其他更改即可按预期工作。
我正在尝试使用 H2 DB 为我的微服务编写 JUNIT 测试用例。我有一些 .sql 文件用于数据库的初始设置,比如创建模式、表等。我在这里看到的一个问题是 .sql 文件被多次调用,每个 JUNIT 一次测试文件。有什么办法可以处理这种情况吗?我的数据库设置文件如下所示 -
@Configuration
@ComponentScan(basePackages={"com.sample.repository"})
public class SampleDBConfig {
JdbcTemplate jdbcTemplate = null;
DataSource dataSource = null;
@Bean
public JdbcTemplate jdbcTemplate()
{
if(jdbcTemplate == null){
LOGGER.info("JdbcTemplate is null, so calling to create ");
jdbcTemplate = new JdbcTemplate(getDataSource());
}else{
LOGGER.info("JdbcTemplate is already set");
}
return jdbcTemplate;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
DataSource getDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
if(dataSource == null){
dataSource = builder
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:folder/initial-setup.sql")
.build();
}
return dataSource;
}
}
我的一个 JUNIT 测试 class 看起来像这样 -
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SampleDBConfig.class)
public class XyxImplTest{
@Autowired
ClassInTestImpl classInTestImpl ;
@Test
public void testMethod(){
.......
}
}
其中一个 class 测试是这样的 -
@Component
@Transactional(rollbackFor = Exception.class)
public class ClassInTestImpl implements ClassInTest {
@Autowired
private JdbcTemplate jdbcTemplate;
.....
我遇到了同样的问题。我所做的快速修复是,我 din 配置数据源 bean。在创建数据源的属性文件中添加了相关的 spring 属性。
而且我在第一次测试中使用了@Sql("classpath:data1.sql"),所以脚本运行只在那个测试套件中使用了一次。它对我有用。
我知道这是一个有点老的话题,但如果其他人有类似的问题,请添加一些注释。我遇到了同样的问题,当 H2 尝试为每个测试文件插入数据时,我遇到了唯一约束违规错误。
最后是最新的H2版本导致的。如果将 H2 版本从 1.4.200 更改为 1.4.199,效果会很好。无需其他更改即可按预期工作。