我正在使用Cucumber在我的应用程序中自动测试服务和控制器。另外,@RunWith(Cucumber.class) 在测试步骤中,我正在使用Cucumber Junit运行程序。我需要实例化我的控制器,并且想知道是否可以使用Spring自动装配它。下面的代码显示了我想要做什么。
@RunWith(Cucumber.class)
public class MvcTestSteps { //is it possible to do this ???? @Autowired private UserSkillsController userSkillsController; /* * Opens the target browser and page objects */ @Before public void setup() { //insted of doing it like this??? userSkillsController = (UserSkillsController) appContext.getBean("userSkillsController"); skillEntryDetails = new SkillEntryRecord(); }
我用黄瓜jvm将Spring自动接线到黄瓜中。
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-spring</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency>
并将applicationContext.xml导入Cucumber.xml
<import resource="/applicationContext.xml" /> // refer to appContext in test package <context:component-scan base-package="com.me.pos.**.it" /> // scan package IT and StepDefs class
在CucumberIT.java中,调用@RunWith(Cucumber.class)并添加CucumberOptions。
CucumberOptions
@RunWith(Cucumber.class) @CucumberOptions( format = "pretty", tags = {"~@Ignore"}, features = "src/test/resources/com/me/pos/service/it/" //refer to Feature file ) public class CucumberIT { }
在CucumberStepDefs.java可以@Autowired春
@Autowired
@ContextConfiguration("classpath:cucumber.xml") public class CucumberStepDefs { @Autowired SpringDAO dao; @Autowired SpringService service; }