验证Spock模拟是否具有指定的超时 [英] Verify Spock mock with specified timeout

查看:82
本文介绍了验证Spock模拟是否具有指定的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mockito中,有一个选项可以验证是否已调用模拟方法,并指定该验证的超时时间(VerificationWithTimeout),例如:

In Mockito there is option to verify if mock method has been called, and specify timeout for this verification (VerificationWithTimeout), for example:

verify(mock, timeout(200).atLeastOnce()).baz();

在Spock中有什么等效功能?

It there any equivalent to such functionality in Spock?

推荐答案

我试图使用PollingConditions来满足类似的情况(这没有帮助),但是在Spock的

I was trying to use PollingConditions to satisfy a similar scenario (which wasn't helpful), but instead found satisfaction in Spock's BlockingVariables. To verify that SomeService.method() is invoked at least once in function ClassBeingTested.method() within a given timeout period:

def "verify an interaction happened at least once within 200ms"(){
    given:
        def result = new BlockingVariable<Boolean>(0.2) // 200ms
        SomeService someServiceMock = Mock()
        someServiceMock.method() >> {
            result.set(true)
        }
        ClassBeingTested clazz = new ClassBeingTested(someService: someServiceMock)
    when:
        clazz.someMethod()
    then:
        result.get()
}

设置结果后,将满足阻塞条件,并且result.get()必须返回true才能通过该条件.如果未能在200毫秒内设置,则测试将失败,并出现超时异常.

When the result is set, the blocking condition will be satisfied and result.get() would have to return true for the condition to pass. If it fails to be set within 200ms, the test will fail with a timeout exception.

这篇关于验证Spock模拟是否具有指定的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆