Cucumber Spring Test @ConfigurationProperties not injecting properties to fields of object

2.6k Views Asked by At

I develop springboot + cucumber and selenium based test atuomation. My spring cucumber infrastructure formed from 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{}

My property class is

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

My application.yml is

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

My Step Definition is

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
}

When I annotate my step definition with @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

}

Now spring inject application property but IntelliJ give me an error: could not autowire. no beans 'somePage' of type found .

dependencies are:

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 version is 2.0.3.RELEASE

1

There are 1 best solutions below

0
On

Newer Spring Boot auto-enables @ConfigurationProperties so a vanilla @SpringBootTest just works. Through cucumber-junit .. the same thing doesn't work and must be configured "the old way" by adding @EnableConfigurationProperties onto your test-step context.

Cucumber 7 example:

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