0

I believe this is a very particular case, but I am building some cucumber tests for some third-party applications we use.

Since I am not really testing my own application, I created a maven project and configured cucumber to run in the main folder (not the test folder).

This is my entrypoint class:

@SpringBootApplication
public class ExecutableMain implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ExecutableMain.class, args);
    }

    @Override
    public void run(String... args) {
        // args logic...
        JUnitCore.runClasses(MyCucumberTest.class);
    }
}

And my test class:

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "html:target/cucumber", "json:target/cucumber/cucumber.json"},
        glue = {"cucumber.app", "cucumber.steps"}
        )
public class MyCucumberTest {

    @AfterClass
    public static void tearDown(){
        // quit the browser
    }
}

This currently works fine, but I want to add spring features to my tests. Specifically, I want to autowire something in my cucumber steps.

Stepdefs:

public class MyStepdefs {

    @Autowired
    private ConfigProperties properties;

    @Given("^Something")
    public void example() {
        //...
    }

I searched around and found people saying I should add the ContextConfiguration annotation in the steps. I did it like so:

@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
    public class MyStepdefs {

But this resulted in a loop during start up.

Can I achieve what I need?

Saita
  • 964
  • 11
  • 36

1 Answers1

0

Ok, so I got it to work following https://stackoverflow.com/a/37586547/1031162

Basically I changed:

@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)

To:

@ContextConfiguration(classes = ExecutableMain.class, initializers = ConfigFileApplicationContextInitializer.class)

I am not 100% sure how/why it worked, but it did.

Saita
  • 964
  • 11
  • 36