我使用springtestdbunit为我的repository / dao编写测试用例。 我使用db脚本填充数据,然后与expected-sql-scripts匹配。
我想为我的示例sql脚本设置转义字符。
我的示例测试类看起来像这样
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
@DbUnitConfiguration(dataSetLoader = ReplacementDataSetLoader.class)
@DatabaseSetup({"notification-init.xml"})
@SpringBootTest(classes = {TestApplication.class, DaoConfig.class})
public class NotificationRepositoryTest {
@Autowired
private NotificationRepository notificationRepository;
@After
@DatabaseTearDown
public void tearDown() throws Exception {
}
@Before
public void setUp() throws Exception {
//donorModel = donorRepository.findOne(0L);
}
我在配置H2数据库时想要设置这两个属性。
DatabaseConfig.PROPERTY_ESCAPE_PATTERN
DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS
答案 0 :(得分:1)
以下是使用JavaConfig设置这两个属性的Spring启动项目的示例:
@Configuration
@Profile("test")
public class DBUnitConfiguration {
@Bean
public DatabaseConfigBean dbUnitDatabaseConfig() {
DatabaseConfigBean bean = new DatabaseConfigBean();
bean.setAllowEmptyFields(true);
bean.setEscapePattern("\"?\"");
return bean;
}
@Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(
DatabaseConfigBean dbUnitDatabaseConfig,
DataSource dataSource
) {
DatabaseDataSourceConnectionFactoryBean bean = new DatabaseDataSourceConnectionFactoryBean(dataSource);
bean.setDatabaseConfig(dbUnitDatabaseConfig);
return bean;
}
}
它需要1.3.0版本的spring-test-dbunit来设置ALLOW_EMPTY_FIELDS道具:
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>