简体   繁体   English

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

[英]Use main application datasource in cucumber test module - Spring-boot application

I was trying to insert data into database in my cucumber tests module in spring-boot application.我试图在 spring-boot 应用程序的黄瓜测试模块中将数据插入数据库。

When start spring app with test profile ( mvn spring-boot:run -Dspring-boot.run.profiles=test ) it start up the application and run properly.当使用测试配置文件( mvn spring-boot:run -Dspring-boot.run.profiles=test )启动 spring 应用程序时,它会启动应用程序并正常运行。 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.问题是在黄瓜测试执行期间,当尝试设置datasource (如下面代码中的 ** 行指出),它为空。 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资源文件夹下的application-test.yml文件,包含测试模块中的所有 spring 数据源

app-db-url: jdbc:oracle:....
app-db-user: USERNAME
spring:
  datasource:
    password: PWD

I went through below solution as well我也经历了以下解决方案

Solution-1 解决方案1

Solution-2 解决方案2

Deployment module app-config.yml部署模块app-config.yml

....
data:
    # Database
    app-db-url : @@app-db-url@@
    app-db-user: @@app-db-user@@
......

It looks like you are missing code that defines that DataSource bean.看起来您缺少定义该 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.其余的属性可以进入属性文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM