Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wkorando
Last active February 21, 2019 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkorando/ae40ec895248dff6ddcecf2ab86ad364 to your computer and use it in GitHub Desktop.
Save wkorando/ae40ec895248dff6ddcecf2ab86ad364 to your computer and use it in GitHub Desktop.
@TestMethodOrder(OrderAnnotation.class)
public class TestTempDir {
@TempDir
static Path classTempDir;
@TempDir
static File classTempDirAsFile;
@Test
@Order(1)
public void useAsClassValue() throws IOException {
File file = classTempDir.resolve("temp.txt").toFile();
FileUtils.write(file, "A", StandardCharsets.ISO_8859_1, true);
assertEquals("A", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
@Test
@Order(2)
public void useAsClassValuePart2() throws IOException {
File file = classTempDir.resolve("temp.txt").toFile();
FileUtils.write(file, "B", StandardCharsets.ISO_8859_1, true);
assertEquals("AB", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
@Test
@Order(3)
public void injectAsMethodValue(@TempDir Path argumentTempDir) throws IOException {
File file = argumentTempDir.resolve("temp.txt").toFile();
FileUtils.write(file, "C", StandardCharsets.ISO_8859_1, true);
assertEquals("ABC", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
@Test
@Order(4)
public void injectAsMethodValuePart2(@TempDir Path argumentTempDir) throws IOException {
File file = argumentTempDir.resolve("temp.txt").toFile();
FileUtils.write(file, "D", StandardCharsets.ISO_8859_1, true);
assertEquals("ABCD", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
@Test
@Order(5)
public void useAsClassFileValue() throws IOException {
File file = new File(classTempDirAsFile, "temp.txt");
FileUtils.write(file, "E", StandardCharsets.ISO_8859_1, true);
assertEquals("ABCDE", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
@Test
@Order(6)
public void injectAsMethodFileValue(@TempDir File tempFile) throws IOException {
File file = new File(classTempDirAsFile, "temp.txt");
FileUtils.write(file, "F", StandardCharsets.ISO_8859_1, true);
assertEquals("ABCDEF", FileUtils.readFileToString(file, StandardCharsets.ISO_8859_1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment