2014-05-09 46 views
10

我正在使用Cucumber在我的應用程序中自動執行服務和控制器的測試。另外,我在測試步驟中使用了Cucumber Junit轉輪@RunWith(Cucumber.class) 。我需要實例化我的控制器,並想知道我是否可以使用Spring來自動裝配它。下面的代碼顯示了我想要做的事情。我可以在黃瓜測試中使用spring自動控制器嗎?

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

} 
+2

與自動裝配在春季黃瓜,JVM在這裏解釋 - http://liminescence.blogspot.com/2013/08/integration-testing-with-spring.html – nilesh

回答

13

我用cucumber-jvm自動裝入春黃瓜。

<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

@RunWith(Cucumber.class) 
@CucumberOptions(
    format = "pretty", 
    tags = {"[email protected]"}, 
    features = "src/test/resources/com/me/pos/service/it/" //refer to Feature file 
) 
public class CucumberIT { } 

在CucumberStepDefs.java可以@Autowired

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

    @Autowired 
    SpringDAO dao; 

    @Autowired 
    SpringService service; 
} 
4

除了上面寶石的回答,不要忘記在自己的附加彈簧的依賴添加爲「黃瓜春」神器似乎只提供黃瓜與春季的「膠水」。

在我的情況下,我得到了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 

我的步驟定義:

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

    @Autowired 
    private ServiceToInject serviceToInject; 
} 

CukesRunner

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

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

<?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> 
8

我只是做了黃瓜和春天與基於java的配置工作,我認爲這是值得分享。 這裏我做了什麼:

@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 

the mav恩依賴我有我的POM是:

<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> 
+2

鑑於我爲黃瓜和春天發現的文檔非常稀少,這非常有幫助。 – PCalouche

+0

這是非常有幫助的。你能分享你的依賴文件嗎? –

+0

我編輯它,我希望它有幫助 – ttati