Spock單元測試快速入門

Spock單元測試入門

導入Maven依賴

<!-- Junit單元測試 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<!-- Spock依賴 -->
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.3-groovy-2.4</version>
    <scope>test</scope>
</dependency>

<!-- Spock需要的groovy依賴 -->
<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.15</version>
</dependency>

<!-- spring boot spock -->
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.2-groovy-2.4</version>
    <scope>test</scope>
</dependency>
        

創建第一個Spock單元測試類

 新建一個Groovy Class,命名爲Test。
在這裏插入圖片描述

/**
 * spock單測類
 */
class Test extends Specification{
    //before setup
    def setupSpec() {
        println ("setupSpec");
    }
    //setup
    def setup() {
        println ("setup");
    }
    //cleanup
    def cleanup() {
        println ("cleanup");
    }
    //after cleanup
    def cleanupSpec() {
        println ("cleanupSpec");
    }
    //測試方法
    def "testMethod"(){
        println "hello world"
        expect:"期望結果"
        1<2
    }
    
}

 點擊運行testMethod()方法,即可看到控制檯輸出:
在這裏插入圖片描述

在這裏插入圖片描述

setup、cleanup方法介紹

 通過控制檯的輸出,可以看到setup()在測試方法執行前運行,可以進行一些測試前的預熱操作。cleanup()在測試方法之後運行,可以用作一些掃尾操作。類似於@Before和@After。

 而setupSpec()方法在setup()之前執行,cleanupSpec()在cleanup()方法之後執行。可以理解爲對setup()和cleanup()方法的增強。

spock語句塊介紹

expect

def "method1"() {
        expect:"期望結果"
        Math.max(1, 3) == 3
        Math.max(7, 4) == 7
        Math.max(0, 0) == 0
    }

 expect用於期望結果的校驗,比如對比兩個數的最大值,1,3的最大值爲3,符合期望結果,測試通過。如果執行結果和期望結果不符合,則報錯。

    def "method1"() {
        expect:"期望結果"
        Math.max(1, 3) == 1
        Math.max(7, 4) == 7
        Math.max(0, 0) == 0
    }

執行報錯
在這裏插入圖片描述

given----expect組合

    def "method2"() {
        given:"準備變量"
        def a = 1
        def b = 2
        
        expect:"期望結果"
        a < b
    }

 given語句塊通常用於單測前的一些數據準備操作,配合expect一起使用。

given----when----then組合

    def "method3"(){
        given:"準備變量"
        def num1=-10
        def num2=-2

        when:"調用方法"
        def res1= Math.abs(num1)
        def res2= Math.abs(num2)

        then:"期望結果"
        res1==10
        res2==2
    }

 given:單測前的準備
 when:當調用方法取得結果
 then:判斷是否符合期望結果

given----when----then----where組合

    def "method4"(){
        given:"準備變量"
        String str="aaabbbcd"

        when:"調用方法"
        def res1=str.contains(str1)
        def res2=str.contains(str2)

        then:"期望結果"
        res1==true
        res2==true

        where:
        str1    |str2
        "aaa"   |"bbb"
        "ab"    |"bc"
    }

 where用於多數據依次測試。when語句塊中並未對str1和str2變量賦值,如果我們需要多組數據進行測試的話,可以使用where語句塊,用表格的形式依次給變量進行賦值。

@Unroll 標籤的使用

    @Unroll
    def "method4"(){
        given:"準備變量"
        String str="aaabbbcd"

        when:"調用方法"
        def res1=str.contains(str1)
        def res2=str.contains(str2)

        then:"期望結果"
        res1==true
        res2==true

        where:
        str1    |str2
        "aaa"   |"bbb"
        "ab"    |"bc"
        "ccdf"  |"g"
    }

 method4()方法新增一組測試用例"ccdf" |“g”,該組用例會導致測試結果與期望不符,如果方法體上加上@Unroll註解,在控制檯可以更具體的看到報錯信息,會顯示具體第幾組數據測試失敗。
在這裏插入圖片描述

與SpringBoot結合使用

@SpringBootTest
class Test extends Specification{
    @Autowired
    UserService userService

    @Unroll
    @Transactional
    @Rollback
    def "method5"(){
        given:
        User user=new User()
        user.setId(id)
        user.setName(name)
        userService.insert(user)

        when:
        User res=userService.getById(id)

        then:
        res.getId()==id
        res.getName()==name

        where:
        id<<[1,2,3]
        name<<["張三","李四","趙五"]
        
//		類似於這種寫法
//        id  |name
//        1   |"張三"
//        2   |"李四"
//        3   |"趙五"
    }
}

 在測試類頂部加上@SpringBootTest標籤,即可將Spock測試類與SpringBoot進行結合,這樣我們可以在測試類中進行@Autowired等操作,通過Spring容器獲得service類,進行功能測試。還可以配合@Transactional和@Rollback註解,使方法體內的數據庫操作通過事務的方式執行,並且測試完後進行回滾,避免造成髒數據。
 個人拙見,不足望指。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章