5

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?

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • According to the link `Resource vs Autowired` they both are not recommended since spring 3.0 - so consider to move on to `interface Inject` annotation from JSR-330. – Artem Oboturov May 13 '12 at 15:42

1 Answers1

5

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

Mikk
  • 551
  • 4
  • 9