SpringBoot实践之---整合Cucumber(黄瓜测试框架)

原文地址: spring boot整合Cucumber(BDD)

1、新建一个springboot工程工程结构如下:


2、添加pom依赖

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.   
  6.     <groupId>com.chhliu</groupId>  
  7.     <artifactId>spring-boot-cucumber</artifactId>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.     <packaging>jar</packaging>  
  10.   
  11.     <name>spring-boot-cucumber</name>  
  12.     <description>Demo project for Spring Boot and Cucumber</description>  
  13.   
  14.     <parent>  
  15.         <groupId>org.springframework.boot</groupId>  
  16.         <artifactId>spring-boot-starter-parent</artifactId>  
  17.         <version>1.5.6.RELEASE</version>  
  18.         <relativePath /> <!-- lookup parent from repository -->  
  19.     </parent>  
  20.   
  21.     <properties>  
  22.         <cucumber.version>1.2.4</cucumber.version>  
  23.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  24.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  25.         <java.version>1.7</java.version>  
  26.     </properties>  
  27.   
  28.     <dependencies>  
  29.         <dependency>  
  30.             <groupId>org.springframework.boot</groupId>  
  31.             <artifactId>spring-boot-starter-web</artifactId>  
  32.         </dependency>  
  33.   
  34.         <dependency>  
  35.             <groupId>info.cukes</groupId>  
  36.             <artifactId>cucumber-java</artifactId>  
  37.             <version>${cucumber.version}</version>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>info.cukes</groupId>  
  41.             <artifactId>cucumber-core</artifactId>  
  42.             <version>${cucumber.version}</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>info.cukes</groupId>  
  46.             <artifactId>cucumber-spring</artifactId>  
  47.             <version>${cucumber.version}</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>info.cukes</groupId>  
  51.             <artifactId>cucumber-junit</artifactId>  
  52.             <version>${cucumber.version}</version>  
  53.             <exclusions>  
  54.                 <exclusion>  
  55.                     <groupId>junit</groupId>  
  56.                     <artifactId>junit</artifactId>  
  57.                 </exclusion>  
  58.             </exclusions>  
  59.         </dependency>  
  60.   
  61.         <dependency>  
  62.             <groupId>org.springframework.boot</groupId>  
  63.             <artifactId>spring-boot-starter-test</artifactId>  
  64.             <scope>test</scope>  
  65.         </dependency>  
  66.     </dependencies>  
  67.   
  68.     <build>  
  69.         <plugins>  
  70.             <plugin>  
  71.                 <groupId>org.springframework.boot</groupId>  
  72.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  73.                 <configuration>  
  74.                     <source>1.7</source>  
  75.                     <target>1.7</target>  
  76.                 </configuration>  
  77.             </plugin>  
  78.   
  79.             <plugin>  
  80.                 <groupId>org.codehaus.mojo</groupId>  
  81.                 <artifactId>exec-maven-plugin</artifactId>  
  82.                 <configuration>  
  83.                     <source>1.7</source>  
  84.                     <target>1.7</target>  
  85.                 </configuration>  
  86.                 <executions>  
  87.                     <execution>  
  88.                         <phase>integration-test</phase>  
  89.                         <goals>  
  90.                             <goal>java</goal>  
  91.                         </goals>  
  92.                         <configuration>  
  93.                             <classpathScope>test</classpathScope>  
  94.                             <mainClass>com.chhliu.test.CucumberTest.java</mainClass>  
  95.                             <arguments>  
  96.                                 <argument>--plugin</argument>  
  97.                                 <argument>pretty</argument>  
  98.                                 <argument>--glue</argument>  
  99.                                 <argument>src/test/resources/</argument>  
  100.                             </arguments>  
  101.                         </configuration>  
  102.                     </execution>  
  103.                 </executions>  
  104.             </plugin>  
  105.         </plugins>  
  106.     </build>  
  107. </project>  

2、编写service接口及其实现类

[java]  view plain  copy
  1. package com.chhliu.service;  
  2.   
  3. /** 
  4.  * 模拟登录 
  5.  * @author chhliu 
  6.  * 
  7.  */  
  8. public interface UserInfoServiceI {  
  9.     boolean login(String username, String password, String confirmPassword);  
  10. }  
[java]  view plain  copy
  1. package com.chhliu.service;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. @Service("userInfoService")  
  6. public class UserInfoService implements UserInfoServiceI{  
  7.     public boolean login(String username, String password, String confirmPassword){  
  8.         return (username.equals("chhliu") && password.equals("123456") && confirmPassword.equals("123456"));  
  9.     }  
  10. }  

3、编写feature文件

[java]  view plain  copy
  1. #language: zh-CN  
  2. #"zh-CN": {  
  3. #    "but""*|但是<",  
  4. #    "and""*|而且<|并且<|同时<",  
  5. #    "then""*|那么<",  
  6. #    "when""*|当<",  
  7. #    "name""Chinese simplified",  
  8. #    "native""简体中文",  
  9. #    "feature""功能",  
  10. #    "background""背景",  
  11. #    "scenario""场景|剧本",  
  12. #    "scenario_outline""场景大纲|剧本大纲",  
  13. #    "examples""例子",  
  14. #    "given""*|假如<|假设<|假定<"  
  15. #  }  
  16.   
  17. @bank  
  18. 功能:假如我在银行取钱的时候,如果我登录成功并且输入的密码正确,那么会显示我的银行卡余额,假如余额为50万  
  19.     场景:银行取钱  
  20.         假如:我以"chhliu"登录  
  21.         并且:输入的密码为"123456"  
  22.         当:确认密码也为"123456"时  
  23.         那么:显示银行卡余额为"500000"  

4、编写测试类

[java]  view plain  copy
  1. package com.chhliu.test;  
  2.   
  3. import org.junit.runner.RunWith;  
  4.   
  5. import cucumber.api.CucumberOptions;  
  6. import cucumber.api.junit.Cucumber;  
  7.   
  8. /** 
  9.  * @RunWith(Cucumber.class) 这是一个运行器 ,指用Cucumber来运行测试 
  10.  * @CucumberOptions中的features,用于指定我们项目中要运行的feature的目录 
  11.  * @CucumberOptions中的format,用于指定我们项目中要运行时生成的报告,并指定之后可以在target目录中找到对应的测试报告 
  12.  * @CucumberOptions中的glue,用于指定项目运行时查找实现step定义文件的目录 
  13.  *  
  14.  * 在实际项目中,随着项目的进行,一个测试工程可能由多个feature文件组成,并且每个feature文件中可能也是由多个scenario组成。默认情况下, 
  15.  * 每次运行是运行所有feature中的所有scenario。这样可能导致正常情况下运行一次测试脚本,需要非常长的时间来等待测试结果。 
  16.  * 但是实际过程中,测试用例是有优先级等区分的。比如smokeTest、regressionTest等。或者有时候会有特别小部分的用例,比如等级是critical, 
  17.  * 这些用例需要长时间运行来监测系统是否没有白页或者页面404等现象。 
  18.  * 所以我们必须区分开所有的scenario,可以使我们在启动测试脚本时,可以根据我们需要来运行哪些模块的scenaro。这时我们可以使用Tags 
  19.  * 在Cucumber里Tag是直接在Feature、Scenari或Scenario Outline关键字前给feature或scenario添加任意数量的前缀为@的tags,多个tag用空格来分隔 
  20.  * @author chhliu 
  21.  * 
  22.  */  
  23. @RunWith(Cucumber.class)  
  24. @CucumberOptions(plugin = {"json:target/cucumber.json""pretty"}, features = "src/test/resources")  
  25. public class CucumberTest {  
  26. }  

5、运行测试类,并对测试输出的未定义步骤进行完善

[java]  view plain  copy
  1. package com.chhliu.test;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.junit.Assert;  
  6.   
  7. import com.chhliu.service.UserInfoServiceI;  
  8.   
  9. import cucumber.api.java.zh_cn.假如;  
  10. import cucumber.api.java.zh_cn.当;  
  11. import cucumber.api.java.zh_cn.那么;  
  12.   
  13. public class Cucumber集成spring {  
  14.       
  15.     @Resource(name="userInfoService")  
  16.     private UserInfoServiceI service;  
  17.       
  18.     private String username;  
  19.       
  20.     private String password;  
  21.       
  22.     private String confirmPassword;  
  23.       
  24.     @假如("^:我以\"([^\"]*)\"登录$")  
  25.     public void 我以_登录(String arg1) throws Throwable {  
  26.         this.username = arg1;  
  27.     }  
  28.       
  29.     @假如("^:输入的密码为\"([^\"]*)\"$")  
  30.     public void 输入的密码为(String arg1) throws Throwable {  
  31.         this.password = arg1;  
  32.     }  
  33.   
  34.     @当("^:确认密码也为\"([^\"]*)\"时$")  
  35.     public void 确认密码也为_时(String arg1) throws Throwable {  
  36.         this.confirmPassword = arg1;  
  37.     }  
  38.   
  39.     @那么("^:显示银行卡余额为\"([^\"]*)\"$")  
  40.     public void 显示银行卡余额为(String arg1) throws Throwable {  
  41.         boolean isLogin = service.login(username, password, confirmPassword);  
  42.         if(isLogin){  
  43.             System.out.println("登录成功!查询余额如下:"+arg1);  
  44.             Assert.assertEquals("500000", arg1);  
  45.         }  
  46.     }  
  47. }  

6、在测试步骤上添加注解支持

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration // 不加此注解,bean会注入不进去  
  3. @SpringBootTest // 不加此注解会找不到bean  
  4. public class Cucumber集成spring{  
  5.   
  6. }  

7、测试结果

[java]  view plain  copy
  1. 2 Scenarios (2 passed)  
  2. 11 Steps (11 passed)  
  3. 0m0.091s  

8、整合注意点

spring boot与Cucumber整合的时候,有个地方需要注意,因为spring boot提倡去xml化,所以传统方式下,Cucumber会读取classpath下的cucumber.xml配置文件来初始化bean的方式,和spring整合后,就不能用这种方式了,需要使用@ContextConfiguration注解来实现类的加载,如果是需要加载配置文件的方式的话,可以如下使用:

[java]  view plain  copy
  1. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })  


如果使用注解的方式来整合的话,使用如下:

[java]  view plain  copy
  1. @ContextConfiguration(classes=SpringBootCucumberApplication.class)  
或者直接

[java]  view plain  copy
  1. @ContextConfiguration  


特别注意:@ContextConfiguration注解必加,否则会出现bean注入失败

下面,我们从源码来看下为什么会造成这种情况。

该部分涉及的代码都在cucumber-spring包下的SpringFactory类中,重点我们看下下面这个类:

[java]  view plain  copy
  1. public void start() {// cucumber测试启动方法  
  2.         if (stepClassWithSpringContext != null) {// 如果使用了@ContextConfiguration注解的话,此处不为null  
  3.             testContextManager = new CucumberTestContextManager(stepClassWithSpringContext);  
  4.         } else {// 否则stepClassWithSpringContext就为null,会进入下面这个分支  
  5.             if (beanFactory == null) {  
  6.                 beanFactory = createFallbackContext();// 这个方法是我们要跟的重点  
  7.             }  
  8.         }  
  9.         notifyContextManagerAboutTestClassStarted();  
  10.         if (beanFactory == null || isNewContextCreated()) {  
  11.             beanFactory = testContextManager.getBeanFactory();  
  12.             for (Class<?> stepClass : stepClasses) {  
  13.                 registerStepClassBeanDefinition(beanFactory, stepClass);  
  14.             }  
  15.         }  
  16.         GlueCodeContext.INSTANCE.start();  
  17.     }  

我们在来跟下createFallbackContext方法:

[java]  view plain  copy
  1. private ConfigurableListableBeanFactory createFallbackContext() {  
  2.         ConfigurableApplicationContext applicationContext;  
  3.         if (getClass().getClassLoader().getResource("cucumber.xml") != null) {// 会先根据classpath下的cucumber.xml来初始化<span style="font-family:Arial, Helvetica, sans-serif;">ConfigurableApplicationContext</span>  
  4.             applicationContext = new ClassPathXmlApplicationContext("cucumber.xml");  
  5.         } else {// 如果没有配置cucumber.xml的话,会new GenericApplicationContext  
  6.             applicationContext = new GenericApplicationContext();  
  7.         }  
  8.         applicationContext.registerShutdownHook();  
  9.         ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();  
  10.         beanFactory.registerScope(GlueCodeScope.NAME, new GlueCodeScope());  
  11.         for (Class<?> stepClass : stepClasses) {  
  12.             registerStepClassBeanDefinition(beanFactory, stepClass);  
  13.         }  
  14.         return beanFactory;  
  15.     }  

最后,来说下GenericApplicationContext这个类,该类会根据Bean的Type类型,然后newInstance实例,但是由于这个类中又注入了其他的类,而注入的类是无法通过new实例的方式来初始化的,所以最后就会注入失败,报空指针了。
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值