Can I use spring to autowire controller in cucumber test?(我可以在黄瓜测试中使用弹簧自动连接控制器吗?)
问题描述
我正在使用 Cucumber 自动测试我的应用中的服务和控制器.另外,我正在使用 Cucumber Junit runner @RunWith(Cucumber.class)在测试步骤中.我需要实例化我的控制器,并且想知道是否可以使用 Spring 来自动装配它.下面的代码显示了我想要做什么.
I am using Cucumber to automate the testing of services and controllers in my app. In addition, I am using the Cucumber Junit runner @RunWith(Cucumber.class)
in the test steps. I need to instantiate my controller and was wondering if I could use Spring to autowire this. The code below shows what I want to do.
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();
}
推荐答案
我使用 cucumber-jvm 将 Spring 自动装配到 Cucumber 中.
I used cucumber-jvm to autowire Spring into Cucumber.
<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
And import applicationContext.xml into 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.
In CucumberIT.java call @RunWith(Cucumber.class) and add the 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 Spring
In CucumberStepDefs.java you can @Autowired Spring
@ContextConfiguration("classpath:cucumber.xml")
public class CucumberStepDefs {
@Autowired
SpringDAO dao;
@Autowired
SpringService service;
}
这篇关于我可以在黄瓜测试中使用弹簧自动连接控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在黄瓜测试中使用弹簧自动连接控制器吗?
基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
