簡體   English   中英

Java - Spock“where”塊不起作用

[英]Java - Spock "where" block is not working

我正在嘗試用 where 編寫一些測試,但似乎沒有傳遞 where 塊中提到的數據(我發現這些值為空)。 這是我的單元測試:

    def "method response should contain count as expected" () {
        given:
        SomeServiceImpl service = applicationContext.getBean(SomeServiceImpl.class)

        when:
        mockConnector.getResponse(userId) >> responseData
        service.setTokenConnector(mockConnector)
        ResponseData res = tokenService.getAllData(userId)
        def count = ((ListResponseMeta)(res.getMeta())).getCount()

        then:
        count == expected

        where:
        responseData | expected
        tokenInfos | 1
        null | 0
    }

tokenInfos先前被初始化為具有某些值的對象數組。

    @Shared
    @AutoCleanup
    Info[] tokenInfos = null

    def setup() {
        tokenInfos = getMockInfoBody()
        mockTokenConnector = Mock(SampleConnector.class)
    }

    private static Info[] getMockInfoBody() {
        Info infoDeactivated = new Info("123", "http://abc.xyz.com", "D")
        Info infoActive = new Info("234", "http://abc.xyz.com", "A")
        Info infoSuspended = new Info("235", "http://abc.xyz.com", "S")

        Info[] tokenInfos = new Info[3]
        tokenInfos[0] = infoDeactivated
        tokenInfos[1] = infoActive
        tokenInfos[2] = infoSuspended

        return tokenInfos
    }

我嘗試在given塊中使用先前的responseData數據塊when移動響應數據。 請在這里幫忙。

我會嘗試回答,但正如@krigaex 指出的那樣,如果沒有最小的、完整的和可驗證的示例,就很難確定。

有很多事情是錯誤的或無效的。

  1. @AutoCleanup將調用字段對象的close()方法。 這里,字段是一個數組,沒有close()方法。
  2. 您將@Shared tokenInfos但您只在第一次setup()調用中初始化它,這對於where塊中的第一個條目來說為時已晚。 因此,要么直接初始化該字段,要么將分配移至setupSpec
    @Shared
    Info[] tokenInfos = getMockInfoBody()
    // OR
    def setupSpec() {
        tokenInfos = getMockInfoBody()
    }

目前,你的方法基本上是這樣的


        where:
        responseData | expected
        null         | 1        // tokenInfos is still null as setup() didn't run yet
        null         | 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM