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?

1
  • 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. Commented May 13, 2012 at 15:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.