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?
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With