Search code examples
javaspringtestingcucumber-jvmserenity-bdd

AutoWiring steps with Spring Cucumber Serenity


I'm failing miserably to auto wire some steps. To illustrate the point, I made a small sample project on github

https://github.com/lpicquet/serenity-cucumber-spring

I am trying to autowire steps so that I can share data between them but the test is currently failing. Anyone can help?


Solution

  • The issue is that you are using a different Runner. Generally people use the SpringRunner.class which handles the ability to create the test context etc.

    Construct a new SpringRunner and initialize a TestContextManager to provide Spring testing functionality to standard JUnit 4 tests.

    To use a different runner along with spring functionality you can use a combination of a ClassRule and a Rule

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
    
    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();
    

    SpringClassRule is a custom JUnit TestRule that supports class-level features of the Spring TestContext Framework in standard JUnit tests by means of the TestContextManager and associated support classes and annotations.

    In contrast to the SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of any Runner and can therefore be combined with existing alternative runners like JUnit's Parameterized or third-party runners such as the MockitoJUnitRunner.

    In order to achieve the same functionality as the SpringJUnit4ClassRunner, however, a SpringClassRule must be combined with a SpringMethodRule, since SpringClassRule only supports the class-level features of the SpringJUnit4ClassRunner.

    Without these there is no ability to Autowire within your dependant classes etc.

    I've added a PR to your project with passing tests.