Groovy Spock BlockingVariable从未发布

时间:2018-09-25 07:21:08

标签: unit-testing grails groovy spock

我在Grails应用程序中与Spock单元测试进行了一场松散的战斗。我想测试异步行为,为了熟悉Spock的BlockingVariable,我编写了这个简单的示例测试。

void "test a cool function of my app I will not tell you about"() {
    given:
    def waitCondition = new BlockingVariable(10000)
    def runner = new Runnable() {
        @Override
        void run() {
            Thread.sleep(5000)
            waitCondition.set(true)
        }
    }

    when:
    new Thread(runner)

    then:
    true == waitCondition.get()
}

不幸的是,这不是一件好事,因为否则它将会终结。当我在Thread.sleep()处设置断点并调试测试时,永远不会命中该断点。我想念什么?

1 个答案:

答案 0 :(得分:2)

您的测试已失败,因为您实际上没有运行您创建的线程。相反:

when:
new Thread(runner)

您应该这样做:

when:
new Thread(runner).run()

然后大约5秒钟后测试成功。