2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 《Java Testing with Spock》_4写单元测试

《Java Testing with Spock》_4写单元测试

时间:2021-08-22 13:08:08

相关推荐

《Java Testing with Spock》_4写单元测试

4.1测试方法中的Spock结构

def "2加3可以得到5"() {//given块及其描述given: "两个整数,2和3"int a = 3int b = 2//when块及其描述when: "把他们相加"int result = a + b//then块及其描述then: "结果是5"result == 5}

除了常用的given-when-then结构意外,Spock还提供了其他几个语义,如下图所示:

4.4 setup

def "装有一种产品的购物篮的重量跟产品相同(替代)"() {//准备单元测试。setup: "一个空购物篮和一台电视"Product tv = new Product(name:"bravia",price:1200,weight:18)Basket basket = new Basket()//触发将要测试的动作。when: "用户想购买电视"basket.addProduct(tv)//检查结果。then: "购物篮重量等于电视"basket.currentWeight == tv.weight}

这里感觉和given没啥区别。

4.8 and

def "有三个产品的购物篮的重量是产品重量之和"() {//given块仅处理被测类。given: "一个空购物篮"Basket basket = new Basket()//and块创建协作者。and: "几种产品"Product tv = new Product(name:"bravia",price:1200,weight:18)Product camera = new Product(name:"panasonic",price:350,weight:2)Product hifi = new Product(name:"jvc",price:600,weight:5)when: "用户想购买电视,摄像机和高保真音响"basket.addProduct tvbasket.addProduct camerabasket.addProduct hifithen: "购物篮重量等于所有产品重量之和"basket.currentWeight == (tv.weight + camera.weight + hifi.weight)}

def "有三个产品的购物篮的重量是产品重量之和(替代)"() {given: "一个空购物篮,一台电视,一台照相机和一个高保真音响"Basket basket = new Basket()Product tv = new Product(name:"bravia",price:1200,weight:18)Product camera = new Product(name:"panasonic",price:350,weight:2)Product hifi = new Product(name:"jvc",price:600,weight:5)//原始when块when: "用户想购买电视.."basket.addProduct tv//扩展when块and: "..相机.."basket.addProduct cameraand: "..和wifi"basket.addProduct hifithen: "购物篮重量等于所有产品重量之和"basket.currentWeight == (tv.weight + camera.weight + hifi.weight)}

def "装有三种产品的购物篮的重量和数量是正确的(有争议)"() {given: "一个空购物篮,一台电视,一台照相机和一个高保真音响"Basket basket = new Basket()Product tv = new Product(name:"bravia",price:1200,weight:18)Product camera = new Product(name:"panasonic",price:350,weight:2)Product hifi = new Product(name:"jvc",price:600,weight:5)when: "用户想购买电视,摄像机和高保真音响"basket.addProduct tvbasket.addProduct camerabasket.addProduct hifi//原始的when块then: "购物篮的重量等于所有产品的重量"basket.currentWeight == (tv.weight + camera.weight + hifi.weight)//扩展then块and: "它包含3个产品"basket.productTypesCount == 3}

4.13 expect

def "有两个产品的购物篮的重量是产品重量之和(前提条件)"() {given: "一个空购物篮,一台电视和一台照相机"Product tv = new Product(name:"bravia",price:1200,weight:18)Product camera = new Product(name:"panasonic",price:350,weight:2)Basket basket = new Basket()//expect块执行中间断言expect:"里面什么也不要"basket.currentWeight == 0basket.productTypesCount == 0when: "用户想购买电视和相机"basket.addProduct tvbasket.addProduct camera//then块检查最终结果then: "购物篮重量等于相机和电视"basket.currentWeight == (tv.weight + camera.weight)}

4.14 cleanup

def "装有一种产品的购物篮的重量跟产品相同"() {given: "一个空购物篮和一台电视"Product tv = new Product(name:"bravia",price:1200,weight:18)Basket basket = new Basket()when: "用户想购买电视"basket.addProduct(tv)then: "购物篮重量等于电视"basket.currentWeight == tv.weight//cleanup块将始终运行,即使when失败cleanup: "刷新购物篮物品"//then块检查最终结果。basket.clearAllProducts()}

4.21 Spock生命周期

class LifecycleSpec extends spock.lang.Specification{//初始化一个代价昂贵(耗资源)的对象def setupSpec() {println "Will run only once"} //所有测试的通用代码def setup() {println "Will run before EACH feature"} //测试方法def "测试第一个功能"() {expect: "trivial test"println "first feature runs" 2 == 1 +1} //测试方法def "测试第二个功能"() {expect: "trivial test"println "second feature runs" 5 == 3 +2} //所有测试的公共清理代码def cleanup() {println "Will run once after EACH feature"} def cleanupSpec() {println "Will run once at the end"} }

输出:

Will run only once

Will run before EACH feature

first feature runs

Will run once after EACH feature

Will run before EACH feature

second feature runs

Will run once after EACH feature

Will run once at the end

可见setupSpec和cleanupSpec只会执行一次,而setup和cleanup在每个测试方法前后都会执行。

4.22 @Shapred注解

class SharedSpec extends spock.lang.Specification{//只创建一次@SharedCreditCardProcessor creditCardProcessor;//会创建多次BillableBasket basket//初始化代价昂贵/速度慢的对象def setupSpec() {creditCardProcessor = new CreditCardProcessor()}//共享对象可以正常使用def setup() {basket = new BillableBasket()creditCardProcessor.newDayStarted()basket.setCreditCardProcessor(creditCardProcessor)}def "用户购买单一产品"() {given: "一个购物车和一台电视机"Product tv = new Product(name:"bravia",price:1200,weight:18)and: "用户想购买电视机" basket.addProduct(tv)when: "用户登出" basket.checkout()then: "获得收入即为电视价格" creditCardProcessor.currentRevenue == tv.price}def "用户购买两样产品"() {given: "一个购物车和一台相机"Product camera = new Product(name:"panasonic",price:350,weight:2)and: "用户想购买两台相机" basket.addProduct(camera,2)when: "用户登出" basket.checkout()then: "取得收入为两种产品的价格"creditCardProcessor.currentRevenue == 2 * camera.price}//运行多次def cleanup() {basket.clearAllProducts()}//只运行一次def cleanupSpec() {creditCardProcessor.shutDown()} }

可以使用@Shared注解,这样可以确保Spock仅创建一次。

4.30 使用闭包来进行判断

def "用Groovy闭包的普通测试"() {given: "一个装有产品的列表"List<String> products= ["camera", "laptop", "hifi"]expect: "相机应该是其中之一"products.any{productName -> productName == "camera"} //迭代列表并传递任何名为camera的值and: "热狗不应该在其中"products.every{productName -> productName != "hotdog"} //迭代列表并检查所有产品的名称}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。