转载: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. IOS中div contenteditable=true无法输入 fastclick.js在点击一个可输入的div时,ios无法正常唤起输入法键盘

    原文地址: https://blog.csdn.net/u010377383/article/details/79838562 前言 为了提升移动端click的响应速度,使用了fastclick.js ...

  2. 2t3ik、ddgs与Linux异常文件下载处理

    异常1: 这样的邮件发生了两周了,烦得很.进入服务器,用top看来下进程. 解决办法 首先 kill 相关PID 进入/tmp/   删除相关文件 rm -rf 2t3ik相关文件 不给相关文件修改权 ...

  3. 深入理解String.intern()方法

    首先进入intern()的源码中, 首先说一点:1.7后的JVM为String在方法区中开辟了一个字符串常量池,如果一个String()不是new()出来的,都将在常量池中拿字符. 注释翻译过来就是, ...

  4. Ionic-wechat项目边开发边学(二):目录结构,header标签与路由

    之前一直跟Linux驱动打交道,上层应用几乎为零,业余时间也不是很多,所以博客也不会写的非常详细,大家有问题尽管评论哦, 我有空会及时回复! 摘要 上一篇文章主要介绍了ionic的开发环境配置, 以及 ...

  5. 转:Spring学习笔记---Spring Security登录页

    转:http://axuebin.com/blog/2016/06/21/spring-security/?utm_source=tuicool&utm_medium=referral. 提示 ...

  6. 鬼题Ghost [manacher]

    本题目来自five20的周末考试题. Description 给定一个 0/1 序列,求其中满足 " ⺉ " 性质的子串个数. " ⺉ " 性质解释: &quo ...

  7. 在mac上无法使用Android Studio的解决方法

    随着android Studio 1.0的正式发布,估计使用的人会越来越多,并且官网上现在已经没有融合好的eclipse下载了,官方推荐下载android Studio.之前的beta版我也安装过,好 ...

  8. 编译 Windows 版本的 Unity Mono(2017-03-12 20:59)

    上一篇说了如何编译 Android 下的 mono,这里简要说下编译 windows 版本的 mono,就是 mono.dll,Unity 版本只有一个 mono.dll,官方的 mono,好几个可执 ...

  9. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  10. Activity(活动)生命周期--系统回收活动数据存储

    当一个活动进入停止状态的时候,是有可能被系统回收的.那如果处于停止状态的活动被系统回收了,而它上面却有我们所需要数据该如何保存呢?(类似于:打开qq进入下一个界面没有进去,返回的时候仍然不需要你输入账 ...