Unit testing Groovy Spock BlockingVariable从未发布
我正在与Grails应用程序中的Spock单元测试进行一场失败的战斗。我想测试异步行为,为了熟悉Spock的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() {
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
的一种手段。