英文:
Use main application datasource in cucumber test module - Spring-boot application
问题
我尝试在我的Spring Boot应用程序的Cucumber测试模块中向数据库插入数据。
当使用测试配置文件启动Spring应用程序(mvn spring-boot:run -Dspring-boot.run.profiles=test
)时,它能够正常启动应用程序并运行。问题出在Cucumber测试执行期间,尝试设置datasource
(代码中标有**的行),它的值变成了null。所以,我应该重新设置数据源吗?如果是这样,应该如何设置?
这不是Cucumber测试相关的问题,问题是我无法访问在主应用程序中设置的数据源。
以下是代码:
@ContextConfiguration(classes = MainApp.class, loader = SpringBootContextLoader.class)
@ActiveProfiles("test")
@Configuration
@PropertySource({"classpath:create-sql.xml"})
public class TestHelper {
@Value("${CreateSql}")
private String CreateSql;
@Autowired
private SqlQueryBuilder sqlQueryBuilder;
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
private UserPreferenceFormatter formatter;
@Autowired
private DataSource dataSource;
public static void getDataList() throws IOException {
MapSqlParameterSource sqlParamSource = new MapSqlParameterSource();
sqlQueryBuilder = new SqlQueryBuilder();
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); ****
String parsedSql = sqlQueryBuilder.parseSql(CreateSql,null,null,null);
List<DataSummary> dataSummaries = jdbcTemplate.query(parsedSql, sqlParamSource, new DataSummaryRowMapper(null,formatter));
}
}
在资源文件夹下的application-test.yml
文件中包含了所有在测试模块中使用的Spring数据源配置:
app-db-url: jdbc:oracle://...
app-db-user: USERNAME
spring:
datasource:
password: PWD
我也查看了下面的解决方案:
英文:
I was trying to insert data into database in my cucumber tests module in spring-boot application.
When start spring app with test profile (mvn spring-boot:run -Dspring-boot.run.profiles=test
) it start up the application and run properly. The issue is during cucumber test execution when try to setup the datasource
(as pointed out ** line in the code below) it comes as null. So should I setup the datasource again? If so how.
It's not cucumber test related issue, The issue is I can't access the datasource which have set in the main app.
Below is the code
@ContextConfiguration(classes = MainApp.class, loader = SpringBootContextLoader.class)
@ActiveProfiles("test")
@Configuration
@PropertySource({"classpath:create-sql.xml"})
public class TestHelper {
@Value("${CreateSql}")
private String CreateSql;
@Autowired
private SqlQueryBuilder sqlQueryBuilder;
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
private UserPreferenceFormatter formatter;
@Autowired
private DataSource dataSource;
public static void getDataList() throws IOException {
MapSqlParameterSource sqlParamSource = new MapSqlParameterSource();
sqlQueryBuilder = new SqlQueryBuilder();
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); ****
String parsedSql = sqlQueryBuilder.parseSql(CreateSql,null,null,null);
List<DataSummary> dataSummaries = jdbcTemplate.query(parsedSql, sqlParamSource, new DataSummaryRowMapper(null,formatter));
}
application-test.yml
file under resources folder with all spring datasources within test module
app-db-url: jdbc:oracle:....
app-db-user: USERNAME
spring:
datasource:
password: PWD
I went through below solution as well
Deployment module app-config.yml
....
data:
# Database
app-db-url : @@app-db-url@@
app-db-user: @@app-db-user@@
......
答案1
得分: 1
以下是翻译好的内容:
似乎您缺少定义 DataSource bean 的代码。
您应该有类似以下的代码:
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
或类似这样的代码:
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
其余的属性可以放入属性文件中。
英文:
It looks like you are missing code that defines that DataSource bean.
You should have something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-java -->
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
<!-- end snippet -->
or something like that:
<!-- language: lang-java -->
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
<!-- end snippet -->
and the rest of the propertied can go into a property file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论