[英]Camel testing with Thread.sleep()
在《骆驼在行动》一书中,我找到了这个测试示例:
@Test
public void testMoveFile() throws Exception {
// create a new file in the inbox folder with the name hello.txt and containing Hello World as body
template.sendBodyAndHeader("file://target/inbox", "Hello World", Exchange.FILE_NAME, "hello.txt");
// wait a while to let the file be moved
Thread.sleep(2000);
// test the file was moved
File target = new File("target/outbox/hello.txt");
assertTrue("File should have been moved", target.exists());
// test that its content is correct as well
String content = context.getTypeConverter().convertTo(String.class, target);
assertEquals("Hello World", content);
}
显然,这是一个集成测试-我们测试多个单元,但是:
我同意大铝。 如果可以避免,则不应该使用Thread.sleep;如果这样做(在测试前端行为时这很常见),您肯定要循环一段指定的时间,如果未在X中完成则失败毫秒。
在我看来,使用线程睡眠往往会创建脆弱或缓慢的测试套件。 您无法保证环境是否会在特定时间内运行测试。
另外,我建议您不要使用单元测试来检查文件是否已移动。 这意味着您的测试依赖于处于特定状态的文件系统,并且从技术上讲并不是在测试最小的东西。
单元测试必须根据定义:
使用Cucumbers来测试此行为或进行其他更高级别的测试会更好。 要么模拟出与文件系统的交互。
那么它的鸡肉和鸡蛋。 就像在《骆驼在行动》一书中一样,稍后我们将向您介绍NotifyBuilder,它使您无需线程休眠即可进行测试。 参见第6章,第6.4.2节。
Camel网站http://camel.apache.org/notifybuilder.html上还有一些有关NotifyBuilder的细节。
我将建立一个循环,不进行睡眠就进行第一次检查(避免不必要的睡眠); 如果文件尚不存在,请睡眠并重复最多N次,如果未在足够的时间内创建文件,则失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.