JUnitの中に一時ディレクトリ内のファイルを追加する方法

ヴィッキー:

私はJUnitの中に一時ディレクトリを作成する2つの方法を発見しました。

ウェイ1:

@Rule
public TemporaryFolder tempDirectory = new TemporaryFolder();

@Test
public void testTempDirectory() throws Exception {
    tempDirectory.newFile("test.txt");
    tempDirectory.newFolder("myDirectory");
    // how do I add files to myDirectory?
}

ウェイ2:

@Test
public void testTempDirectory() throws Exception {
    File myFile = File.createTempFile("abc", "txt");
    File myDirectory = Files.createTempDir();
    // how do I add files to myDirectory?
}

コメントは、上記に言及したように、私は私がこれらの一時ディレクトリにいくつかの一時ファイルを追加する必要性を持っています。このような構造と終了時に最終的には削除のすべてに対して、私のテストを実行します。

それ、どうやったら出来るの?

サーシャShpota:

あなたはそれをあなたが本当のフォルダのためにそれを行うのと同じ方法を行うことができます。

@Rule
public TemporaryFolder rootFolder = new TemporaryFolder();

@Test
public void shouldCreateChildFile() throws Exception {
    File myFolder = rootFolder.newFolder("my-folder");

    File myFile = new File(myFolder, "my-file.txt");
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=233942&siteId=1