2017-02-19 42 views
1

我正在使用springtestdbunit編寫我的資源庫/ dao的測試用例。 我使用數據庫腳本填充數據,然後與預期的sql腳本匹配。如何在彈簧數據集中設置默認屬性D​​atabaseConfig

我想爲我的示例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

下面是使用JavaConfig設置這些2個屬性從一個彈簧引導項目的例子:

@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版本彈簧體檢查的DbUnit設置ALLOW_EMPTY_FIELDS支柱:

<dependency> 
    <groupId>com.github.springtestdbunit</groupId> 
    <artifactId>spring-test-dbunit</artifactId> 
    <version>1.3.0</version> 
    <scope>test</scope> 
</dependency> 
相關問題