J'utilise Cucumber pour automatiser les tests de services et de contrôleurs dans mon application. De plus, j'utilise le runner Cucumber Junit @RunWith(Cucumber.class)
dans les étapes de test. J'ai besoin d'instancier mon contrôleur et je me demandais si je pouvais utiliser Spring pour câbler automatiquement cela. Le code ci-dessous montre ce que je veux faire.
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();
}
J'ai utilisé cucumber-jvm pour câbler automatiquement Spring dans 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>
Et importez applicationContext.xml dans 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
Dans CucumberIT.Java, appelez @RunWith(Cucumber.class)
et ajoutez 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 { }
Dans CucumberStepDefs.Java, vous pouvez @Autowired
Spring
@ContextConfiguration("classpath:cucumber.xml")
public class CucumberStepDefs {
@Autowired
SpringDAO dao;
@Autowired
SpringService service;
}
Je viens de faire travailler le concombre et le printemps avec Java et je pense qu'il vaut la peine de partager ici. Voici ce que j'ai fait:
@Configuration
@ComponentScan(basePackages = { "com.*" })
@PropertySource("classpath:application-${environment}.properties")
public class AppConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
ma classe de définition d'étape:
@ContextConfiguration(classes= AppConfiguration.class)
public class Stepdefs {
@Autowired
private MyBean mybean;
et ici la classe de test:
@RunWith(Cucumber.class)
@CucumberOptions(format = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" })
@ContextConfiguration(classes= AppConfiguration.class)
public class RunCucumberTest {
}
et la commande maven est:
mvn -Denvironment=dev clean test
les dépendances maven que j'ai dans mon pom sont:
<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>
En plus de la réponse de Jewel ci-dessus, n'oubliez pas d'ajouter vos propres dépendances de ressort supplémentaires, car l'artefact "concombre-ressort" ne semble fournir que la "colle" entre le concombre et le printemps.
Dans mon cas, j'ai obtenu Spring Injection pour travailler dans mes définitions d'étape avec la dépendance supplémentaire suivante.
<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
Ma définition de l'étape:
@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>