5

I'm using Spring, Cucumber and Junit in my project. The test runner is annotated with @ContextConfiguration so it creates a Spring context before running the suite.

I would like Spring to instantiate the Step Definition classes as Spring beans so I can inject my dependencies and use them in the steps. It can be done by declaring the class as @Component.

So far so good. The problem is that Cucumber will instantiate the step definitions again and will not use the beans already created by Spring with the dependencies injected.

Any ideas to make Cucumber use the Step definitions instantiated by Spring?

Eugene S
  • 6,709
  • 8
  • 57
  • 91
Juan Luis
  • 3,317
  • 4
  • 17
  • 23
  • possible duplicate of [Can I use spring to autowire controller in cucumber test?](http://stackoverflow.com/questions/23564938/can-i-use-spring-to-autowire-controller-in-cucumber-test) – Boj Jan 15 '15 at 03:10

1 Answers1

0

Your runner class should be annotated with @RunWith(Cucumber.class) rather that with @ContextConfiguration. Cucumber should then scan and instantiate your Spring context accordingly.

Eugene S
  • 6,709
  • 8
  • 57
  • 91