Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Groovy Spock BlockingVariable从未发布_Unit Testing_Grails_Groovy_Spock - Fatal编程技术网

Unit testing Groovy Spock BlockingVariable从未发布

Unit testing Groovy Spock BlockingVariable从未发布,unit-testing,grails,groovy,spock,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() {

我正在与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()
设置一个断点并调试测试时,该断点从未被命中。我遗漏了什么?

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

when:
new Thread(runner)
你应该做:

when:
new Thread(runner).run()

大约5秒钟后,您的测试成功。

啊,我怎么会错过这个?谢谢我会为这个愚蠢的错误责骂自己三次。你可以使用(在Groovy中)
Thread.start{waitCondition.set(true)}
来帮助避免这种错误,因为你不需要记住调用
run()
:-)谢谢,在以后的无意义测试中,我会尽量记住这一点:)这里的目标不是使用
Thread
,它只是测试
BlockingVariable
的一种手段。