gpt4 book ai didi

java - Cucumber Spring 测试 @ConfigurationProperties 没有将属性注入(inject)对象的字段

转载 作者:行者123 更新时间:2023-12-05 06:32:30 25 4
gpt4 key购买 nike

我开发了 springboot + Cucumber 和基于 selenium 的测试自动化。我的春 cucumber 基础设施由 https://github.com/cucumber/cucumber-jvm/tree/master/spring 形成.

@RunWith(Cucumber.class)
@CucumberOptions(
plugin = "json:out/cucumber.json",
features = {"classpath:features"},
glue = {"my.package"})
public class SeleniumCukes{}

我的属性(property)等级是

@Data
@Configuration
@ConfigurationProperties(prefix = "application")
public class ApplicationProperty {
private String baseUrl;
}

我的application.yml是

application:
base-url: https://www.google.com/

我的步骤定义是

public class SomeStep{
@Autowired
private SomePage somePage;

@Autowired
private ApplicationProperty applicationProperty;

@Given("^Go to Some Page$")
public void catalogUserIsOnTheLoginPage() throws Throwable {
somePage.navigateTo("some url");
applicationProperty.getBaseUrl(); //Cucumber spring do not inject configuration property here.
}
...etc
}

当我用@SpringBootTest 注释我的步骤定义时

@SpringBootTest
public class SomeStep{
@Autowired
private SomePage somePage;

@Autowired
private ApplicationProperty applicationProperty;

@Given("^Go to Some Page$")
public void catalogUserIsOnTheLoginPage() throws Throwable {
somePage.navigateTo("some url");
applicationProperty.getBaseUrl(); //Cucumber spring do inject configuration property here.
}
...etc

}

现在 spring 注入(inject)应用程序属性,但 IntelliJ 给我一个错误:无法 Autowiring 。没有找到类型的 bean “somePage”。

依赖项是:

dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.seleniumhq.selenium:selenium-server:3.13.0')
compile('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.cucumber:cucumber-java:2.4.0')
testCompile('io.cucumber:cucumber-junit:2.4.0')
testCompile('io.cucumber:cucumber-spring:2.4.0')
testCompile('org.springframework:spring-tx')

}Spring Boot 版本为 2.0.3.RELEASE

最佳答案

较新的 Spring Boot 自动启用 @ConfigurationProperties,因此 Vanilla @SpringBootTest 就可以正常工作。通过 cucumber-junit .. 同样的事情不起作用,必须通过将 @EnableConfigurationProperties 添加到您的测试步骤上下文中来配置“旧方法”。

cucumber 7 示例:

@CucumberContextConfiguration
@EnableConfigurationProperties // <<< Add this
@SpringBootTest

关于java - Cucumber Spring 测试 @ConfigurationProperties 没有将属性注入(inject)对象的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51248332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com