JUnitのを使用してソースファイルをdestファイルにコピーするメソッドをテストしようとしていますTemporaryFolder
。IOException
ただし、このテストを実行しようとするとJavaが表示されます。フォルダの宣言をどこで行うかは重要ですか?(私のテストクラスには、いくつかの異なるテストがあります)。もしそうなら、それを行うための適切な方法は何ですか?現在、このコードの上にいくつかの単体テストがあるので、ファイルコピーのテストを設定しようとしています。たぶん---ブロックはそれ自身@Rule
のクラスにある必要がありますか?これが私がテストをコーディングしたスニペットです:@Before
@Test
...other tests...then:
@Rule
public static TemporaryFolder tmp = new TemporaryFolder();
private File f1, f2;
@Before
public void createTestData() throws IOException {
f1 = tmp.newFile("src.txt");
f2 = tmp.newFile("dest.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f1));
out.write("This should generate some \n" +
"test data that will be used in \n" +
"the following method.");
out.close();
}
@Test
public void copyFileTest() {
out.println("file 1 length: " + f1.length());
try {
copyFile(f1, f2);
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
}
if (f1.length() != f2.length())
fail();
else if (!f1.equals(f2))
fail();
assertSame(f1, f2);
}
このテストクラスを実行すると、11個のテストすべてが失敗し(以前は合格)、が取得されjava.io.IOException: No such file or directory
ます。