Reputation: 2823
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@@
......
Upvotes: 1
Views: 1694
Reputation: 912
It looks like you are missing code that defines that DataSource bean. You should have something like this:
@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();
}
}
or something like that:
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
and the rest of the propertied can go into a property file.
Upvotes: 1