java - 我可以在 cucumber 测试中使用 Spring 自动连接 Controller 吗?

标签 java spring cucumber cucumber-jvm cucumber-junit

我正在使用 Cucumber 自动测试我的应用中的服务和 Controller 。另外,我正在使用 Cucumber Junit runner @RunWith(Cucumber.class) 在测试步骤中。我需要实例化我的 Controller ,并且想知道是否可以使用 Spring 来 Autowiring 它。下面的代码显示了我想要做什么。

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();

}

最佳答案

我刚刚使用基于 java 的配置制作了 cucumber 和 spring,我认为值得在这里分享。 这是我所做的:

@Configuration
@ComponentScan(basePackages = { "com.*" })
@PropertySource("classpath:application-${environment}.properties")
public class AppConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();

}
}

我的步骤定义类:

@ContextConfiguration(classes= AppConfiguration.class)
public class Stepdefs {
@Autowired
private MyBean mybean;

这里是测试类:

@RunWith(Cucumber.class)
@CucumberOptions(format = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" })
@ContextConfiguration(classes= AppConfiguration.class)
public class RunCucumberTest {
}

而maven命令是:

mvn -Denvironment=dev clean test

我在 pom 中的 maven 依赖项是:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--spring test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

        <!--cucumber -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm</artifactId>
            <version>${cucumber.version}</version>
            <type>pom</type>
        </dependency>

关于java - 我可以在 cucumber 测试中使用 Spring 自动连接 Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564938/

相关文章:

java - Akka Actor 的沙箱和监控

java - JPA - 创建临时主键

java - 如何在方法中接受文本文件?

spring - 使用 Spring MVC 和 REST 提交表单时出现错误

java - 如何使用参数之一的不同值多次运行所有功能文件

ruby - cucumber 调试器没有停止

java - 使用 "deploytool"和Matlab编译器有什么区别?

java - 属性文件导致空 Spring Boot 项目出现异常

Spring 启动, Spring 启动启动器数据jpa : database type NONE or Cannot find changelog location

python - 如何使用beeve(Python BDD框架)设置环境变量?