【问题标题】:How to initialize environment variable in a Spring-Java integration test using Cucumber?如何使用 Cucumber 在 Spring-Java 集成测试中初始化环境变量?
【发布时间】:2021-03-05 19:45:48
【问题描述】:

我在项目的属性文件 (src/test/resources/application.properties) 中添加了以下环境变量:

#variables
  limit=5

我想开发一个集成测试,但它总是出错,因为我的限制变量的值是 0 而不是 5(这是我在属性中设置的值)。我尝试使用@SpringBootTest 和@TestPropertySource 但我不成功,变量保持为零:

@RunWith(Cucumber.class)
@SpringBootTest(properties = {"limit=5"})
@CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
public class RunCucumberTestIT {

}

@RunWith(Cucumber.class)
@TestPropertySource(properties = {"limit=5"})
@CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
public class RunCucumberTestIT {

}

【问题讨论】:

  • 嗯,@SpringBootTest(properties = {"limit=5"}) 应该没问题,奇怪的是它不起作用。你也可以试试@TestPropertySource(locations = "classpath:application.properties")吗?
  • 你好,我试过这个:@TestPropertySource(locations = "classpath:application.properties", properties = {"limit = 5"}) 但变量保持为零

标签: java spring cucumber integration-testing


【解决方案1】:

您正试图通过注释 JUnit 运行器类让 Cucumber 了解您的 Spring 应用程序上下文配置。然而,Cucumber 集成了多个测试框架并拥有自己的 CLI。因此,注释 JUnit 运行器类并不总是有效(例如,当 CLI 时)。

所以要这样做you can annotate a configuration class on your glue path with @CucumberContextConfiguration 和@SpringBootTest。

使用最新版本的 Cucumber (v6.9.0) 这应该可以工作:

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>
package com.example.app;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
public class RunCucumberTestIT {

}
package com.example.app;

import org.springframework.boot.test.context.SpringBootTest;

import io.cucumber.spring.CucumberContextConfiguration;

@CucumberContextConfiguration
@SpringBootTest(properties = {"limit=5"})
public class CucumberSpringConfiguration {

}

注意:配置类的包应该在胶水路径上。未定义时默认为 runner 类的包。

【讨论】:

    【解决方案2】:

    感谢所有试图提供帮助的人。我将分享我是如何解决它的,我实际上不需要使用@TestPropertySource 或@SpringBootTest,我只是更改了在集成测试调用的服务中声明变量的方式。

    之前:

    @Value("${limit}")
    private static int dateFilterLimit;
    

    之后:

    @Value("${limit:5}")
    private int dateFilterLimit;
    

    因此,如果测试无法查看属性中声明的内容,则在变量中设置值 5。对于其余的代码,属性中的变量仍然存在:

    #variables
    limit=5
    

    而配置类是这样的:

    @RunWith(Cucumber.class)
    @CucumberOptions(features = { "classpath:project/feature/list" }, plugin = {"pretty" }, monochrome = true)
    public class RunCucumberTestIT {
    
    }
    

    【讨论】:

    • 您通过设置默认值解决了问题。但似乎您的应用程序没有从属性文件中读取。这不是预期的行为。你确定你解决了这个问题吗?
    • 我同意你的看法。但是我的 cucumber.version 是 4.7.2 所以我不能听从朋友 M.P. 的建议。科尔斯坦杰
    • 啊。将 spring boot 测试注释放在一个包含步骤定义的类上。这很愚蠢,但它有效。
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 2021-08-10
    • 2012-03-13
    • 2022-11-08
    • 2019-10-13
    • 2022-01-21
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多