简体   繁体   English

我可以在黄瓜测试中使用spring来自动控制控制器吗?

[英]Can I use spring to autowire controller in cucumber test?

I am using Cucumber to automate the testing of services and controllers in my app. 我正在使用Cucumber在我的应用程序中自动测试服务和控制器。 In addition, I am using the Cucumber Junit runner @RunWith(Cucumber.class) in the test steps. 另外,我在测试步骤中使用了Cucumber Junit运行程序@RunWith(Cucumber.class) I need to instantiate my controller and was wondering if I could use Spring to autowire this. 我需要实例化我的控制器,并想知道我是否可以使用Spring自动装配它。 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();

}

I used cucumber-jvm to autowire Spring into Cucumber. 我使用cucumber-jvm将Spring自动装入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>

And import applicationContext.xml into Cucumber.xml 并将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

In CucumberIT.java call @RunWith(Cucumber.class) and add the CucumberOptions . 在CucumberIT.java中调用@RunWith(Cucumber.class)并添加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 {  }

In CucumberStepDefs.java you can @Autowired Spring 在CucumberStepDefs.java中你可以@Autowired Spring

@ContextConfiguration("classpath:cucumber.xml")
public class CucumberStepDefs {

    @Autowired
    SpringDAO dao;

    @Autowired
    SpringService service;
}

I just made cucumber and spring working with java based configuration and I think it is worth to share here. 我刚刚使用基于java的配置制作黄瓜和弹簧,我认为值得在这里分享。 here what I have done: 在这里我做了什么:

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

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

}
}

my step defintion class: 我的步骤定义类:

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

and here the test class: 这里是测试类:

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

and the maven command is: 和maven命令是:

mvn -Denvironment=dev clean test

the maven dependencies I have in my pom are: 我在我的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>

In addition to Jewel's answer above do not forget to add in your own additional spring dependencies as "cucumber-spring" artifact seems to only provided the "Glue" between Cucumber and Spring. 除了宝石上面的答案之外,不要忘记添加你自己的额外弹簧依赖性,因为“黄瓜弹簧”神器似乎只提供了Cucumber和Spring之间的“胶水”。

In my case I got Spring Injection to work in my step definitions with the following added dependency. 在我的例子中,我使用Spring Injection在我的步骤定义中使用以下添加的依赖项。

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <scope>test</scope>
</dependency>
..... Plus the rest of the cucumber dependencies

My Step Definition: 我的步骤定义:

@ContextConfiguration("/cucumber.xml")
@PropertySource("classpath*:qa-test.properties")
public class StepsDefinition {

    @Autowired
    private ServiceToInject serviceToInject;
}

CukesRunner CukesRunner

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

Cucumber.xml Cucumber.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <import resource="classpath:applicationContext.xml"/>
</beans>

applicationContext.xml applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.mypackage"/>

    <bean id="propertiesPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="qa-test.properties"/>
    </bean>
    ---- rest of my beans
  </beans>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 Spring 参数化 class 代替 spring 自动装配吗 - can i use Spring parameterized class used instead of spring autowire 用于REST控制器的Spring MVC 4,如何为测试用例自动装配不同的依赖项 - Spring MVC 4 for REST Controller, how to autowire different dependencies for test cases 我可以将 Spring bean 自动装配到 MapStruct 接口中吗? - Can I autowire a Spring bean into a MapStruct interface? 春季:我可以自动装配零部件领域吗? - spring: can I autowire fields of component fields? 我无法在春季自动装配存储库 - I can't autowire repository in spring Spring Boot无法为服务测试类自动装配存储库bean - Spring boot can not autowire repository bean for service test class 无法在Junit测试中自动装配JpaRepository - Spring启动应用程序 - Can't Autowire JpaRepository in Junit test - Spring boot application Spring无法自动连接Controller中的存储库 - Spring not able to autowire repository in Controller Spring Boot的黄瓜测试可以在“mvn test”中运行,但不能在“mvn verify”中运行 - Cucumber test for Spring Boot can run in “mvn test” but not in “mvn verify” 我是否需要在xml文件中显式使用“ autowire”在Spring Web App中自动装配 - do i need to explicitly use “autowire” in xml file to autowire in Spring web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM