Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring dependencies not injected for Cucumber runner

I have existing test cases which use SpringJUnit4ClassRunner which use @Resource annotation to flag variable for injection.

@Resource is used as another DI framework may be used in the future. (@Resource vs @Autowired)

Now I have started writing BDD test cases using the Cucumber runner. However DI does not appear to be happening. (@Autowired works but not @Resource) Anyone know why not?

like image 813
Reimeus Avatar asked May 12 '12 13:05

Reimeus


1 Answers

(I'm assuming you're using Cucumber-JVM)

Instead of using SpringJUnit4ClassRunner, you should use the Cucumber runner instead.

@RunWith(Cucumber.class)

To use this you will need the following dependencies:

    <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>

This will look for cucumber.xml in your class path. This XML is simply a spring bean configuration XML. Mine is pretty straight forward and contains:

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

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

When you run your tests you should see Spring load cucumber.xml and then import context.xml.

like image 195
Tobin Schwaiger-Hastanan Avatar answered Sep 29 '22 07:09

Tobin Schwaiger-Hastanan