我尝试在异步测试中使用阻塞变量,下面的问题
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)
如果我离开睡眠和简单的Assert
这很有效,但我不想
2条答案
按热度按时间rjzwgtxy1#
您可以使用CountDownLatch。
jogvjijk2#
目前我也遇到了这个问题,这是因为voucherService不是一个模拟对象,所以存根不会工作,你应该用@SpringSpy来注解它