没有为Cucumber runner注入Spring依赖项

时间:2012-05-12 13:37:12

标签: java spring dependency-injection bdd cucumber-jvm

我现有的测试用例使用SpringJUnit4ClassRunner,它使用@Resource注释来标记注入变量。

@Resource用作未来可能会使用的另一个DI框架。 (@Resource vs @Autowired

现在我已经开始使用Cucumber跑步者编写BDD测试用例。然而DI似乎没有发生。 (@Autowired有效,但不是@Resource)任何人都知道为什么不呢?

1 个答案:

答案 0 :(得分:5)

(我假设你正在使用Cucumber-JVM)

不应使用 SpringJUnit4ClassRunner ,而应使用 Cucumber 转轮。

@RunWith(Cucumber.class)

要使用此功能,您需要以下依赖项:

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${info.cukes.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${info.cukes.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>${info.cukes.version}</version>
        <scope>test</scope>
    </dependency>

这将在您的类路径中查找 cucumber.xml 。这个XML只是一个spring bean配置XML。我很直接,包含:

<context:component-scan base-package="cucumber.runtime.java.spring"/>
<context:annotation-config/>

<!-- wire beans required for testing -->
<import resource="classpath*:/context.xml"/>

运行测试时,您应该看到Spring加载 cucumber.xml ,然后导入 context.xml