24 Comments

  1. Could you help me out how to make roll back for the DB operation in cucumber?

    1. Author

      I don’t think there is a simple answer to that question… Ideally, each Cucumber test should be isolated and able to load the test data it needs. Then, you could simply delete everything. If you can’t do that, you could also generate data using predefined identifier patterns and then delete selectively after the test.

  2. Author

    Hi! Thanks for the feedback. I updated the post and the source code to cover that use case.

  3. Great article, it helped me a lot. Nevertheless, there’s an issue when having multiple StepDefinitions that extend SpringBootBaseIntegrationTest, as there are multiple definitions of a spring context. More specifically, it outputs this exception:

    cucumber.runtime.CucumberException: Glue class class com.wtvr.FeatureXSteps and class com.wtvr.FeatureYSteps both attempt to configure the spring context. Please ensure only one glue class configures the spring context

  4. Hello, thanks for the walkthrough, we have a similar setup but we now want to extend our step definition to be in separate files for single responsibility. Now when we try to run our tests, we get errors saying that “please ensure that only one glue class configures the spring context”. We believe this is because both step definition files are extending the SpringBootBaseIntegrationTest class. We’ve tried extending the SpringBootBaseIntegrationTest in the BagOfCucumbers class but it doesn’t start up Spring.

    Any ideas how to get round this issue. Thank you.

  5. yes,I check my code agnin ,there is a mistake in my code beacuse I used spring junit 4 starter rather than springboottest in your article, it create spring container every step ,I have solved this problem , thank you.

  6. hi i have done this as the article says,but i found that the spring container would initialization every test step,do you have any ideal to solve this problem,I just want to init the spring container once during my test thanks!

    1. Author

      Is your code shared somewhere to have a look at it? Does your Spring context get recreated for every step, or for every feature?

  7. Thank you for great article, How do you create jar and run from command line ?

    1. Author

      The most common option to run these tests from the command line is to simply use maven so they will run as part of the build.
      $ mvn clean test
      If you really want to run tests from within the jar file, check this link.

  8. Now how do you use @Autowired. It is null in step definitions file when trying to auto wire a Apache Camel ProducerTemplate.

  9. After a lot of researching on web, first example that works cucumber-spring boot integration!

  10. How about running cucumber agains a remote server ?

  11. Thank you so much, this is the one blog post which helped me getting my cucumber test case running with ‘io.cucumber’ dependency. Many other blogs are using ‘info.cukes’ dependencies, even though its years old. Thanks much.

  12. I have a custom runner that extends BlockJUnit4ClassRunner to allow me to inject specific Spring beans into certain tests. I want to be able to inject these beans into my step defs, so I tried building the same structure as your guide but having SpringBootBaseIntegrationTest run with my XYZRunner instead of SpringRunner.

    When I debug the test from BagCucumberIntegrationTest it gets into my step definition functions but it never runs the constructor of XYZRunner. The structure is BagCucumberIntegrationTest extendsSpringBootBaseIntegrationTest, [email protected](XYZRunner.class)`. Do you have any ideas?

    1. Author

      Is it an open source project? Just send me the link and I will have a look, otherwise it’s difficult to figure out what is going wrong… Maybe you could also use different configurations so you get different beans injected into your tests.

  13. Thanks for this great article.
    Do you have an idea of how to make the tests Transactional for Rollback database changes at the end of a scenario. typically in my springboot junit tests i use the Transactional annotation. transactionaltestexecutionlistener does the magic.
    how can i use similar behaviour Here?

    thanks

  14. I unfortunately have been unable to get cucumber to play ball with Spring Boot 2, I have tried using the same cucumber setup you have but I run into an initializationError when I run gradlew test. If I include the @Ignore on my steps class the test just skips completely and there is no output from cucumber. Any thoughts?

    1. Author

      I’m certainly not a gradle expert and I didn’t try to use it in this setup, but have you tried the setup described in this link? You can also point me to the code if it’s public and I can have a look.

  15. its my own code with the same approach that you talked about. I am running the test class with @RunWith(Cucumber.class)

    below is how my step’s parent class looks like.

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

    public class AbstractDefs {

    I had used the same dependencies that you had mentioned.

    1. Author

      I think I know what might be happening, I just reproduced that exception by removing the annotation @SpringBootApplication from the code’s main class (in my case SpringBootCucumberApplication). Check that your code (your main code, not the tests) has a proper configuration (e.g. are you missing the annotation?). Does the application start? If it does, then it might be that some configuration is overridden while running the tests.

  16. very good article. I was also looking for the same solutions. I tried with the approach, but I am getting an error with the below exception.

    java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test

    1. Author

      Are you trying the same code or your own? Check which test class you’re running (the one with @RunWith(Cucumber.class)) and the class annotations (the steps’ parent class is annotated with @SpringBootTest).

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.