1

我开发了基于 springboot + cucumber 和 selenium 的测试自动化。我的 spring 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{}

我的财产类别是

@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

}

现在弹簧注入应用程序属性,但 IntelliJ 给我一个错误:无法自动装配。找不到类型的 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

4

1 回答 1

0

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

黄瓜 7 示例:

@CucumberContextConfiguration
@EnableConfigurationProperties // <<< Add this
@SpringBootTest
于 2022-03-02T15:39:28.027 回答