黄瓜弹簧测试 @ConfigurationProperties不向对象字段注入属性



我开发了基于弹簧靴+黄瓜和硒的测试方法。 我的春参基础设施是由 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{}

我的属性类是

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

我的应用程序.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
}

现在弹簧注入应用程序属性,但 IntelliJ 给我一个错误: 无法自动接线。未找到类型的豆子"某页"。

依赖项包括:

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 引导版本是 2.0.3.RELEASE

较新的Spring Boot会自动启用@ConfigurationProperties因此香草@SpringBootTest就可以了。 通过黄瓜朱尼特..同样的事情不起作用,必须通过向测试步骤上下文添加@EnableConfigurationProperties来"旧方式"进行配置。

黄瓜 7 示例:

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

最新更新