1

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

Solution-1

Solution-2

Deployment module app-config.yml

....
data:
    # Database
    app-db-url : @@app-db-url@@
    app-db-user: @@app-db-user@@
......
SMPH
  • 2,073
  • 8
  • 33
  • 70
  • 1
    You cannot autowire static fields. – Dirk Deyne Aug 23 '20 at 13:21
  • @DirkDeyne I changed the code to make all non static. Still the same results. – SMPH Aug 23 '20 at 21:11
  • Other doubt had was, is this due to access issue of `application-test.yml` under resource folder in test module. How can we verify that? – SMPH Aug 23 '20 at 21:17
  • Further as per suggestions in this https://stackoverflow.com/questions/38711871/load-different-application-yml-in-springboot-test changed the code (updated the question snippets as well). No luck though – SMPH Aug 23 '20 at 21:37
  • BTW, Is this approach make sense? Becuz the main application is `spring-boot ` and my test project is cucumber. So is `@Autowire` will load those instances with above code? – SMPH Aug 24 '20 at 00:44
  • `spring.datasource.url` is missing in your yaml-config file. And don't put a space between your key and the colon... – Dirk Deyne Aug 24 '20 at 05:42
  • @DirkDeyne Not sure what does really mean, I have `app-db-url` as the db connectivity. Corrected the space issue. – SMPH Aug 27 '20 at 06:33
  • copy `app-db-url` to `spring.datasource.url` and `app-db-user` to `spring.datasource.user` but then in yaml style : [yaml config](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-yaml) – Dirk Deyne Aug 27 '20 at 10:21
  • @DirkDeyne The reason being I have another module which is just for deployment purpose. In that module we have `app-config.yml` which define data properties. (Updated question with that) – SMPH Aug 29 '20 at 00:32

1 Answers1

1

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.

Igor Kanshyn
  • 407
  • 3
  • 7
  • My idea is to use exiting main spring-boot project db classes in test project. As I mentioned in my previous comments the doubt I have is, since the test project is not a spring-boot project (it has POJO classes) is that possible to use as per my code in the question. So that I only need separate `application.yml` to have required properties. The way you mentioned above your answer will work as it uses separate bean for db connection. – SMPH Aug 29 '20 at 02:39
  • Further even after implementing the DataSourceBean you mentioned I cannot access the `CreateSql` xml specified in the question code. It comes as null. Currently that xml file reside under main app resources folder. Ideally it needs to be moved to test project folder as it is not part of main app. – SMPH Aug 29 '20 at 02:59
  • `dataSource = new DataSourceBean();` and `jdbcTemplate = new NamedParameterJdbcTemplate(dataSource1.getDataSource());` will crate the datasource but as I mentioned this way it is not re using main app data source. It's separate implementation. But still I can't access `CreateSql.xml` as mentioned above comment. – SMPH Aug 29 '20 at 03:30