2014-05-09 11 views
10

मैं अपने ऐप में सेवाओं और नियंत्रकों के परीक्षण को स्वचालित करने के लिए ककड़ी का उपयोग कर रहा हूं। इसके अलावा, मैं परीक्षण चरणों में ककड़ी जूनिट धावक @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(); 

} 
+2

Autowiring वसंत के साथ ककड़ी-JVM में यहां से समझाया गया है - http://liminescence.blogspot.com/2013/08/integration-testing-with-spring.html – nilesh

उत्तर

13

मैंने ककड़ी-जेवीएम का उपयोग स्प्रिंग इन ककड़ी को स्वचालित करने के लिए किया था।

<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

ऊपर गहना के जवाब के अलावा के रूप में "ककड़ी वसंत" अपनी खुद की अतिरिक्त वसंत निर्भरता में जोड़ने के लिए भूल नहीं है सकते हैं विरूपण साक्ष्य केवल प्रदान की जा रहा है ककड़ी और वसंत के बीच "गोंद"।

मेरे मामले में मुझे निम्नलिखित अतिरिक्त निर्भरता के साथ मेरी चरण परिभाषाओं में काम करने के लिए स्प्रिंग इंजेक्शन मिला।

<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

012,
<?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

मैंने सिर्फ जावा आधारित कॉन्फ़िगरेशन के साथ काम कर रहे ककड़ी और वसंत को बनाया और मुझे लगता है कि यह यहां साझा करना उचित है। यहाँ मैं क्या किया है:

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

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

} 
} 

मेरे कदम defintion वर्ग:

@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 

एमएवी निर्भरता मैं अपने पोम में एन हैं:

<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

संबंधित मुद्दे

 संबंधित मुद्दे