在“行动中的骆驼”一书中,我找到了这个测试示例:
@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)是否被认为是好的做法?
- 我们可以做得更好,离开集成测试方法吗?