Unit testing Hadoop:如何对文件系统进行单元测试
我想运行单元测试,但我需要一个org.apache.hadoop.fs.FileSystem实例。Unit testing Hadoop:如何对文件系统进行单元测试,unit-testing,hadoop,Unit Testing,Hadoop,我想运行单元测试,但我需要一个org.apache.hadoop.fs.FileSystem实例。 创建文件系统是否有任何模拟或其他解决方案?一种可能的方法是在JUnit4.7中使用临时文件夹 请参阅。:或。我所做的(直到找到更好的解决方案)扩展了文件系统。为什么不使用Mockito或PowerMock之类的模拟框架来模拟您与文件系统的交互?单元测试不应该依赖于实际的文件系统,而应该只是在与文件系统交互时验证代码中的行为。看看hadoop测试 <dependency> <
创建文件系统是否有任何模拟或其他解决方案?一种可能的方法是在JUnit4.7中使用临时文件夹
请参阅。:或。我所做的(直到找到更好的解决方案)扩展了文件系统。为什么不使用Mockito或PowerMock之类的模拟框架来模拟您与文件系统的交互?单元测试不应该依赖于实际的文件系统,而应该只是在与文件系统交互时验证代码中的行为。看看hadoop测试
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-test</artifactId>
<version>0.20.205.0</version>
</dependency>
org.apache.hadoop
hadoop测试
0.20.205.0
有设置类<代码> MiniDFSCluster 和
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
在拆卸方法中,您应该关闭mini-hdfs集群,并删除临时目录
hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);
您可能想看看RawLocalFileSystem。尽管我认为你最好还是模仿一下。你可以使用: 添加以下依赖项 查看创建微型集群后的示例日志
我的解决方案是创建一个扩展抽象Hadoop的
DummyFileSystem``文件系统
FileSystem`,这样我就可以伪造文件是否存在,等等。“所有文件都存在”的示例:
我发现更容易控制伪造数据。我用sbt尝试了Thirupathi Chavati和Alexander Tokarev解决方案,并且:
import org.apache.hadoop.hdfs.MiniDFSCluster
将仅通过添加以下内容起作用:
libraryDependencies+=“org.apache.hadoop”%“hadoop hdfs”%“2.8.1“分类器”测试”
libraryDependencies+=“org.apache.hadoop”%”hadoop通用“%”2.8.1“分类器”测试“
这是集成测试。您是否能够成功模拟文件系统
?
hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);
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);
...
}
}
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>2.7.3</version>
<!-- <scope>test</scope>-->
</dependency>
import java.nio.file.{Files, Paths}
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.hdfs.MiniDFSCluster
object MiniClusterDemo extends App {
def sysDir: String = System.getProperty("user.dir")
if(miniCluster!=null) println("Cluster created and active") else println("something went wrong")
def miniCluster: FileSystem = {
val basePath = Paths.get(s"$sysDir")
val baseDir = Files.createTempDirectory(basePath,"hdfs_test").toFile.getAbsoluteFile
val conf = new Configuration()
conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath)
val hdfsCluster = new MiniDFSCluster.Builder(conf).build()
val hdfsURI = s"hdfs://localhost:${hdfsCluster.getNameNodePort}/"
val fileSystem = hdfsCluster.getFileSystem
//hdfsCluster.shutdown();
//FileUtil.fullyDelete(baseDir);
fileSystem
}
}
@Override
public FileStatus getFileStatus(Path f) throws IOException {
return new FileStatus(10, false, 3, 128*1024*1024,1,1, null, null, null, f);
}