Verify Spock mock with specified timeout

前端 未结 4 448
灰色年华
灰色年华 2021-01-04 21:20

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



        
4条回答
  •  自闭症患者
    2021-01-04 21:57

    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(0.2) // 200ms
            SomeService someServiceMock = Mock()
            someServiceMock.method() >> {
                result.set(true)
            }
            ClassBeingTested clazz = new ClassBeingTested(someService: someServiceMock)
        when:
            clazz.someMethod()
        then:
            result.get()
    }
    

    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.

提交回复
热议问题