简体   繁体   English

黄瓜步作为春豆

[英]Cucumber steps as spring beans

I'm using Spring, Cucumber and Junit in my project. 我在我的项目中使用Spring,Cucumber和Junit。 The test runner is annotated with @ContextConfiguration so it creates a Spring context before running the suite. 测试运行器使用@ContextConfiguration注释,因此它在运行套件之前创建Spring上下文。

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. 我希望SpringStep Definition类实例化为Spring bean,这样我就可以注入我的依赖项并在步骤中使用它们。 It can be done by declaring the class as @Component . 可以通过将类声明为@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. 问题是Cucumber会再次实例化步骤定义,并且不会使用Spring创建的bean并注入依赖项。

Any ideas to make Cucumber use the Step definitions instantiated by Spring? 任何想让Cucumber使用Spring实例化的Step定义的想法?

Your runner class should be annotated with @RunWith(Cucumber.class) rather that with @ContextConfiguration . 您的跑步者类应使用@RunWith(Cucumber.class)注释,而不是使用@ContextConfiguration Cucumber should then scan and instantiate your Spring context accordingly. 然后,Cucumber应该相应地扫描并实例化您的Spring上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM