3

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

Can Bezmen
  • 112
  • 6
  • You are missing @ContextConfiguration(...) on your step definitions class declaration. See for example [QandA](https://stackoverflow.com/questions/37859073/cucumber-java-with-spring-boot-integration-spring-autowired-throws-nullpointe) – MikeJRamsey56 Jul 09 '18 at 20:26
  • I have already seen that answer but It doesn't work. @SpringBootTest also initialize spring context but idea give the error that I explained above.. – Can Bezmen Jul 10 '18 at 05:33
  • In that property configuration class include @ConfigurationProperties(prefix = "application.yaml") or else you can use @TestPropertySource("classpath:application.yaml") in the SomeSteps Class which is your @SpringBootTest class – MangduYogii Apr 10 '19 at 13:01

1 Answers1

1

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
drekbour
  • 2,895
  • 18
  • 28