在我的Spring Boot项目中,我有两个数据源:
@Primary
@Bean(name = "pgDatasource")
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.dbcp2.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.dbcp2.url"));
dataSource.setUsername(env.getProperty("spring.datasource.dbcp2.username"));
dataSource.setPassword(env.getProperty("spring.datasource.dbcp2.password"));
dataSource.setMaxActive(Integer.valueOf(env.getProperty("spring.datasource.dbcp2.max-total")));
dataSource.setMaxIdle(Integer.valueOf(env.getProperty("spring.datasource.dbcp2.max-idle")));
dataSource.setInitialSize(Integer.valueOf(env.getProperty("spring.datasource.dbcp2.initial-size")));
return dataSource;
}
@Bean(name = "h2Datasource")
public BasicDataSource h2DataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("spring.h2.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.h2.datasource.url"));
dataSource.setUsername(env.getProperty("spring.h2.datasource.username"));
dataSource.setPassword(env.getProperty("spring.h2.datasource.password"));
Resource initData = new ClassPathResource("scripts/inmem.sql");
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initData);
DatabasePopulatorUtils.execute(databasePopulator, dataSource);
return dataSource;
}
这里PostgreSQL的数据源是主要的bean。在我的几个测试中,我想针对h2数据库运行脚本。为此,我尝试使用@Sql
注释。但是,如果我使用@Sql
,它将针对pgDatasource
运行脚本。我可以将h2配置为这些测试的主bean,但是测试方法的主体取决于pgDatasource
是主bean的配置。
测试样本:
@Test
@Sql(scripts = "/clean_login_attempts.sql", executionPhase = AFTER_TEST_METHOD)
void loginAttemptsIncrementTheCount() throws Exception {
unsuccessfulLoginRequest();
unsuccessfulLoginRequest();
unsuccessfulLoginRequest();
LoginAttempt loginAttempt = loginAttemptService.getAttempt("admin");
assertEquals(3, loginAttempt.getAttempt());
}
是否可以为org.springframework.test.context.jdbc.Sql
注释配置数据源?
答案 0 :(得分:0)
添加config = @SqlConfig(dataSource =“ h2Datasource”,transactionManager =“ h2tx”)解决了该问题。
let name = 'test';
let table = 'table1'; // table id
let uri = 'data:application/vnd.ms-excel;base64,';
let template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>';
let base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)));
};
let format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; });
};
if (!table.nodeType) { table = document.getElementById(table); }
let ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
let hiddenElement = document.createElement('a');
hiddenElement.href = uri + base64(format(template, ctx));
hiddenElement.target = '_blank';
hiddenElement.download = `${name}.xls`;
hiddenElement.click();