使用主应用程序数据源在Cucumber测试模块中 – Spring Boot应用程序

huangapple go评论74阅读模式
英文:

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

我也查看了下面的解决方案:

解决方案-1

解决方案-2

英文:

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(&quot;test&quot;)
    @Configuration
    @PropertySource({&quot;classpath:create-sql.xml&quot;})
    public class TestHelper {
    
        @Value(&quot;${CreateSql}&quot;)
        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&lt;DataSummary&gt; 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@@
......

答案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(&quot;org.h2.Driver&quot;);
        dataSourceBuilder.url(&quot;jdbc:h2:mem:test&quot;);
        dataSourceBuilder.username(&quot;SA&quot;);
        dataSourceBuilder.password(&quot;&quot;);
        return dataSourceBuilder.build();
    }
}

<!-- end snippet -->

or something like that:
<!-- language: lang-java -->

@Bean 
public DataSource getDataSource() { 
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); 
    dataSourceBuilder.username(&quot;SA&quot;); 
    dataSourceBuilder.password(&quot;&quot;); 
    return dataSourceBuilder.build(); 
}

<!-- end snippet -->

and the rest of the propertied can go into a property file.

huangapple
  • 本文由 发表于 2020年8月23日 19:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63546402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定