我想运行单元测试,但我需要有一个org.apache.hadoop.fs.FileSystem实例。是否有任何模拟或任何其他解决方案来创建FileSystem?
发布于 2011-11-30 05:53:30
看一下hadoop-test
jar
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-test</artifactId>
<version>0.20.205.0</version>
</dependency>
它包含用于设置MiniDFSCluster
和MiniMRCluster
的类,因此您可以在没有Hadoop的情况下进行测试
发布于 2015-02-05 17:04:31
如果您使用的是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,并在其上运行测试。setUp方法可能如下所示:
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();
在tearDown方法中,您应该关闭mini hdfs集群,并删除临时目录。
hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);
发布于 2011-11-30 03:48:12
为什么不使用Mockito或PowerMock这样的模拟框架来模拟你与FileSystem的交互呢?您的单元测试不应该依赖于实际的FileSystem,而应该只是在与FileSystem交互时验证代码中的行为。
https://stackoverflow.com/questions/8308828
复制相似问题