我想运行单元测试,但我需要一个org.apache.hadoop.fs.FileSystem实例。 是否有任何模拟或任何其他解决方案来创建FileSystem?Hadoop:如何进行单元测试FileSystem
9
A
回答
6
看一看类Hadoop试验瓶
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-test</artifactId>
<version>0.20.205.0</version>
</dependency>
它归类为建立MiniDFSCluster和MiniMRCluster所以你可以不Hadoop的
2
一种可能的方法是在Junit 4.7中使用TemporaryFolder。
参见:http://www.infoq.com/news/2009/07/junit-4.7-rules或http://weblogs.java.net/blog/johnsmart/archive/2009/09/29/working-temporary-files-junit-47。
0
我做了什么(直到我会找到更好的解决方案)我扩展了FileSystem。
5
为什么不使用像Mockito或PowerMock这样的模拟框架来模拟你与FileSystem的交互?你的单元测试不应该依赖于实际的FileSystem,而应该只是验证代码中与FileSystem交互的行为。
10
如果你正在使用Hadoop 2.0.0和测试以上 - 考虑使用Hadoop的minicluster
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
有了它,你可以在你的本地计算机上的临时HDFS,并在其上运行测试。的设置方法可以是这样的:
baseDir = Files.createTempDirectory("test_hdfs").toFile().getAbsoluteFile();
Configuration conf = new Configuration();
conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath());
MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(conf);
hdfsCluster = builder.build();
String hdfsURI = "hdfs://localhost:"+ hdfsCluster.getNameNodePort() + "/";
DistributedFileSystem fileSystem = hdfsCluster.getFileSystem();
而且在拆卸方法,你应该关闭你的小HDFS集群,并删除临时目录。
hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);
0
您可能想看看RawLocalFileSystem。尽管我认为你最好嘲笑它。
0
您可以使用HBaseTestingUtility:
public class SomeTest {
private HBaseTestingUtility testingUtil = new HBaseTestingUtility();
@Before
public void setup() throws Exception {
testingUtil.startMiniDFSCluster(1);
}
@After
public void tearDown() throws IOException {
testingUtil.shutdownMiniDFSCluster();
}
@Test
public void test() throws Exception {
DistributedFileSystem fs = testingUtil.getDFSCluster().getFileSystem();
final Path dstPath = new Path("/your/path/file.txt);
final Path srcPath = new Path(SomeTest.class.getResource("file.txt").toURI());
fs.copyFromLocalFile(srcPath, dstPath);
...
}
}
相关问题
- 1. 如何单元测试Hadoop的可写
- 2. 如何对wxPython进行单元测试?
- 3. 如何对rxjs5进行单元测试?
- 4. 如何对Response.Redirect进行单元测试?
- 5. 如何对IDataErrorInfo进行单元测试?
- 6. 如何进行单元测试App.OnStartup
- 7. Grails - 如何进行单元测试addTo *
- 8. 如何进行单元测试navigator.notification.alert
- 9. 我如何进行单元测试openWithCompletionHandler
- 10. 如何对Xtext进行单元测试
- 11. OpenGL如何进行单元测试?
- 12. 如何进行单元测试?
- 13. IRepository如何进行单元测试?
- 14. 如何进行单元测试
- 15. 如何对FileContentResult进行单元测试?
- 16. 如何为EXC_BAD_ACCESS进行单元测试?
- 17. 如何进行单元测试配置
- 18. 如何进行单元测试子类
- 19. 如何进行单元测试出错
- 20. 使用HbastTestingUtils进行单元测试hadoop作业:NoSuchMethodError:org.apache.hadoop.hdfs.MiniDFSCluster。 <init>
- 21. 如何使用System.Windows.Threading.Dispatcher对单元进行单元测试
- 22. 如何对与系统(或Android)类进行交互的单元测试方法进行单元测试
- 23. 当进行单元测试以及何时集成测试
- 24. 何时使用测试脚本进行单元测试?
- 25. 单元测试...如何改进它
- 26. 如何对子进程进行单元测试.CalledProcessError
- 27. 如何对二进制方法进行单元测试?
- 28. 如何调试缀有蘑古力进行单元测试
- 29. Hadoop单元测试嘲讽上下文
- 30. 如何在使用嵌套测试类时对单元测试构造函数进行单元测试