【发布时间】:2018-10-22 17:01:25
【问题描述】:
我正在尝试使用阻塞变量进行异步测试,遵循这个问题
Verify Spock mock with specified timeout
我只想知道是否调用了一个结束方法。我尝试复制接受的答案,但从未设置阻塞变量。
阻塞变量只是超时。
所以我有
@TestConfiguration
static class ITRouterConfiguration {
private DetachedMockFactory factory = new DetachedMockFactory()
@Bean
IVoucherService voucherService() {
return factory.Mock(IVoucherService)
}
}
@Autowired
private IVoucherService voucherService
def 'Should create a voucher'() {
MockQueue mockQueue = mockDestinationManager.createQueue("SERVICE_REQUEST")
Assert.assertNotNull(jmsTemplate);
Assert.assertNotNull(mockDestinationManager);
given:
def result = new BlockingVariable<Boolean>(0.2) // 200ms
voucherService.createVoucher() >> {
result.set(true)
}
when: 'a message is sent'
jmsTemplate.send(mockQueue, new MessageCreator() {
@Override
Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(BODY)
message.setStringProperty(ACTION_TYPE, CREATE)
return message
}
})
//sleep(2000)
then: 'check create voucher was called once'
result.get()
}
BlockingVariable.get() timed out after 10.00 seconds
at spock.util.concurrent.BlockingVariable.get(BlockingVariable.java:113)
at uk.co.cpp.servicerequestrouter.CreateVoucherSpec.Should create a voucher(CreateVoucherSpec.groovy:81)
如果我离开睡眠并简单断言
1 * voucherService.createVoucher(_)
它有效,但我不想。
【问题讨论】:
-
为什么
result.get() == WorkResult.OK在then中对你不起作用:spockframework.org/spock/javadoc/1.0/spock/util/concurrent/…? -
顺便说一句,在这个问题中看起来与 Spring Integration 无关:spring.io/projects/spring-integration ...
-
@ArtemBilan 如果您注意到 jmsQueues 和消息,这与它有很多关系。最后一个问题我称它为 spring,并被告知它是 spring 集成。
-
@ArtemBilan 因为在接受的答案中没有指定,所以我没有尝试
-
我认为
200ms太少,无法等待通过 JMS 调用服务...
标签: groovy spring-integration spock