[英]How to test catch block in Java with Spock that has a simple log statement
[英]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 指出的那樣,如果沒有最小的、完整的和可驗證的示例,就很難確定。
有很多事情是錯誤的或無效的。
@AutoCleanup
將調用字段對象的close()
方法。 這里,字段是一個數組,沒有close()
方法。@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.