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 pro