转载:http://blog.csdn.net/hankle_xu/article/details/77531880
spock测试框架,使用groovy作为脚本语言,开发出的测试脚本具有优良的阅读性,通过标签结构化测试代码。groovy的语法简洁强大,可以节省很多代码。闭包很强大。Spock的mock和stub功能比junit的mockito、jmock、easymock都要简单好用,在spring maven工程中,spock-spring起到一个桥梁作用,它集成spock和spring test,从而可以在spock测试框架里测试Java bean。
 
maven 工程结构:测试脚本放在src/test/groovy目录下,在project setting中设置groovy目录为testSources目录
spring 配置文件中声明的bean:
只需在测试类上加注解@ContextConfiguration,spock测试便可access到spring容器,为了验证待测试bean成功注入到spock测试框架中, 我们做如下测试:
 
@ContextConfiguration(locations = "classpath*:spring-config.xml")
class BaseSpec extends Specification {
 
}
 
@Title("跨店铺优惠券测试")
@Subject(CouponWriteService)
class UnionCouponSpec extends BaseSpec {
    @Autowired
    CouponWriteService couponWriteService
 
    def "验证待测接口成功注入"() {
 
        expect: "bean 成功注入"
        couponWriteService instanceof CouponWriteService
 
    }
 
}

 
运行测试,如下测试结果,表明待测bean成功注入到spock测试框架
结合spock-reports组件,spock框架可以生成漂亮的测试报告,包含spoc测试注解,标签注释内容等,还可配置在报告中展示测试代码,只需在报告*.properties文件中设置com.athaydes.spockframework.report.showCodeBlocks=true,下面生成的报告是不是很漂亮
报告详细设置可在 META-INF/services/com.athaydes.spockframework.report.IReportCreator.properties 目录下根据需要灵活配置,包括样式、输出目录、工程名称和版本、报告模板等
 
# Name of the implementation class of the report creator
# Currently supported classes are:
#   1. com.athaydes.spockframework.report.internal.HtmlReportCreator
#   2. com.athaydes.spockframework.report.template.TemplateReportCreator
com.athaydes.spockframework.report.IReportCreator=com.athaydes.spockframework.report.internal.HtmlReportCreator
 
# Set properties of the report creator
# For the HtmlReportCreator, the only properties available are
# (the location of the css files is relative to the classpath):
com.athaydes.spockframework.report.internal.HtmlReportCreator.featureReportCss=spock-feature-report.css
com.athaydes.spockframework.report.internal.HtmlReportCreator.summaryReportCss=spock-summary-report.css
com.athaydes.spockframework.report.internal.HtmlReportCreator.printThrowableStackTrace=false
com.athaydes.spockframework.report.internal.HtmlReportCreator.inlineCss=true
com.athaydes.spockframework.report.internal.HtmlReportCreator.enabled=true
 
# exclude Specs Table of Contents
com.athaydes.spockframework.report.internal.HtmlReportCreator.excludeToc=false
 
# Output directory (where the spock reports will be created) - relative to working directory
com.athaydes.spockframework.report.outputDir=build/spock-reports
 
# If set to true, hides blocks which do not have any description
com.athaydes.spockframework.report.hideEmptyBlocks=false
 
# Set the name of the project under test so it can be displayed in the report
com.athaydes.spockframework.report.projectName=
 
# Set the version of the project under test so it can be displayed in the report
com.athaydes.spockframework.report.projectVersion=Unknown
 
# Show the source code for each block
com.athaydes.spockframework.report.showCodeBlocks=false
 
# Set the root location of the Spock test source code (only used if showCodeBlocks is 'true')
com.athaydes.spockframework.report.testSourceRoots=src/test/groovy
 
# Set properties specific to the TemplateReportCreator
com.athaydes.spockframework.report.template.TemplateReportCreator.specTemplateFile=/templateReportCreator/spec-template.md
com.athaydes.spockframework.report.template.TemplateReportCreator.reportFileExtension=md
com.athaydes.spockframework.report.template.TemplateReportCreator.summaryTemplateFile=/templateReportCreator/summary-template.md
com.athaydes.spockframework.report.template.TemplateReportCreator.summaryFileName=summary.md
com.athaydes.spockframework.report.template.TemplateReportCreator.enabled=true
工程pom文件主要如下配置:
  1. 配置spring 框架依赖,为了spock能使用spring test的ApplicationContext,spring-test.jar必不可少,从而通过注解自动引入待测bean
  2. 配置spock 测试框架相关jar包,spock-core.jar强制引入
  3. 由于spock相对于spring框架是外来户,spock中的内省注解需要引入aspectjrt.jar才能被spring的aop调用
  4. 配置编译、测试、报告等插件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.jd.pop.qa.test.api</groupId>
    <artifactId>api-coupon-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>api-coupon-test</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springframework.version>4.3.9.RELEASE</springframework.version>
    </properties>
 
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>junit</groupId>-->
            <!--<artifactId>junit</artifactId>-->
            <!--<version>4.11</version>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->
 
        <!--system under test!-->
        <dependency>
            <groupId>com.jd.pop.soa.market</groupId>
            <artifactId>pop-market-center-api</artifactId>
            <version>2.0.5-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
 
        <!--springframework configuration!-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
 
        <!--spock and groovy configuration start! -->
 
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
 
        <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.4.4</version>
        <scope>test</scope>
        </dependency>
 
        <!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.1-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
 
        <!-- https://mvnrepository.com/artifact/org.spockframework/spock-spring 方便集成spring test-->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>1.1-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
 
        <!-- https://mvnrepository.com/artifact/com.athaydes/spock-reports -->
        <!--设置报告模板,可参考https://github.com/renatoathaydes/spock-reports 介绍配置
        com.athaydes.spockframework.report.IReportCreator.properties-->
        <dependency>
            <groupId>com.athaydes</groupId>
            <artifactId>spock-reports</artifactId>
            <version>1.3.1</version>
            <scope>test</scope>
        </dependency>
 
        <!-- // if you don't already have slf4j-api and an implementation of it in the classpath, add this! -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.13</version>
            <scope>test</scope>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addTestSources</goal>
                            <!--<goal>compile</goal>-->
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Spec.java</include>
                        <!--<include>**/*Test.groovy</include>-->
                        <!--<include>**/*Spec.groovy</include>-->
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <!--spock and groovy configuration end! -->
 
</project>

spock spring 集成测试框架搭建心得的更多相关文章

  1. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  2. (三) Angular2项目框架搭建心得

    前言: 在哪看到过angular程序员被React程序员鄙视,略显尴尬,确实Angular挺值得被调侃的,在1.*版本存在的几个性能问题,性能优化的"潜规则"贼多,以及从1.*到2 ...

  3. Spring JdbcTemplate框架搭建及其增删改查使用指南

    Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...

  4. 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建

    1. 前言 基于Maven的开发方式开发项目已经成为主流.Maven能很好的对项目的层次及依赖关系进行管理.方便的解决大型项目中复杂的依赖关系.S2SH(Struts2+Spring+Hibernat ...

  5. spring mvc 框架搭建及详解

    现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...

  6. JAVA开发:分享一些SpringMvc+Ibatis+spring的框架使用心得

    近期不在做.net的项目,而是使用java作为开发语言,就想着要用springmvc开发了,由于前些年也用过struts1/2+hibernate/ibatis+spring开发过项目,因此是有些底子 ...

  7. Spring MVC框架搭建

    Spring MVC篇一.搭建Spring MVC框架 本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij ...

  8. Spring boot Spring cloud 框架搭建

    随笔记载几个框架搭建时的坑: 这个是server提供者模块,需要注意的是spring:application:name 接下来是fegin模块,需要主要注意信息已说明,需要特别说明的是RequestM ...

  9. Spring MVC 框架搭建及具体解释

    如今主流的Web MVC框架除了Struts这个主力 外.其次就是Spring MVC了,因此这也是作为一名程序猿需要掌握的主流框架.框架选择多了.应对多变的需求和业务时,可实行的方案自然就多了. 只 ...

随机推荐

  1. 一款你不容错过的Laravel后台管理扩展包 —— Voyager

    http://laravelacademy.org/post/6401.html  Posted on 2016年11月1日 by  学院君 1.简介 Voyager是一个你不容错过的Laravel后 ...

  2. redis三种连接方式

    安装 tar zxvf redis-2.8.9.tar.gz cd redis-2.8.9 #直接make 编译 make #可使用root用户执行`make install`,将可执行文件拷贝到/u ...

  3. NBUT 1220 SPY

    $map$,简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...

  4. SystemProperties cannot be resolved错误

    单独用eclipse打开一个工程,出现SystemProperties.get()会出现SystemProperties cannot be resolved.错误 1. 引用SystemProper ...

  5. Eclipse 主题(修改背景色)

  6. Python安装scrapy提示 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++

    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools&quo ...

  7. 【POJ 3974】Palindrome

    http://poj.org/problem?id=3974 Manacher模板题.Menci的博客讲得很好 有一点:Menci的代码中的right我感觉是代表能延伸到的最右端点的右边的点,因为r( ...

  8. HDU 6041 I Curse Myself(二分+搜索)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6041 [题目大意] 给出一个仙人掌图,求第k小生成树 [题解] 首先找到仙人掌图上的环,现在的问题 ...

  9. 【构造】Gym - 100781B - Bell Ringing

    根据n-1推n的情况,让n从每一个的最右走到最左,再从下一个最左走到最右,如此往复即可. #include<cstdio> using namespace std; int n,a[500 ...

  10. python基础之包与logging模块

    包 1.什么是包? 包是模块的一种形式,包的本质就是一个含有__init__.py文件的文件夹 2.为什么要有包? 提高程序的结构性和可维护性 3.如何使用包? 导入包就是在导包下的__init__. ...