转载: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. Ljava/lang/Iterable与AbstractMethodError

    java.lang.AbstractMethodError: com.example.demo.repository.UserRepositoryImpl.findAll()Ljava/lang/It ...

  2. jdk之object源码理解

    Java所有类都继承与Object,本文谈谈我对object源码的理解,如果有错的,请看官多多批评指正. 1.registerNatives() private static native void ...

  3. STL模板整理 全排列

    概念: 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a, ...

  4. Python—HTTP处理Gzip压缩数据

    HTTP 请求中包含Accept-encoding: gzip头信息可以告诉服务器,如果它有任何新数据要发送给我时,请以压缩的格式发送.如果服务器支持压缩,它将返回由 gzip 压缩的数据并且使用Co ...

  5. 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)

    P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...

  6. Luogu P2486 染色(树链剖分+线段树)

    题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...

  7. web worker 实践

    1.web worker 在浏览器中JavaScript主线程与UI渲染线程是互斥的.即UI渲染线程会阻塞JavaScript线程的运行. web worker允许创建工作线程,并可以与JavaScr ...

  8. Flask实战第68天:项目上线部署

    在开发机上的准备工作 1.确认项目没有bug 2.用pip freeze >requirements.txt将当前环境的包导出到requirements.txt文件中,方便部署的时候安装 3.将 ...

  9. import 和 export -- ES6

    例子: 最后返回的结果是: { default: function fn2(){ }, foo2: 1, test3: { default: function fn3(){ }, foo3: 1 } ...

  10. luogu P2485 [SDOI2011]计算器

    题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y.z.p,计算 ...