How to create a JUnit TemporaryFolder with subfolders

I would like to create a JUnit TemporyFolder that represents the baseFolder of such a tree:

baseFolder/subFolderA/subSubFolder
          /subFolderB/file1.txt

As far as I understand, I can install the TemporaryFolder application and create using the “newFolder ()” pseudo-folders that are in this same folder. But how can I create layers below them? Especially in a way that is cleaned after the test.

+5
source share
2 answers

temporaryFolder.newFolder(String... folderNames)temporaryFolder.newFolder(String... folderNames)hierarchy is used as parameters temporaryFolder.newFolder(String... folderNames)::

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void test() throws Exception {
    File child = temporaryFolder.newFolder("grandparent", "parent", "child"); //...

    assertEquals("child", child.getName());
    assertEquals("parent", child.getParentFile().getName());
    assertEquals("grandparent", child.getParentFile().getParentFile().getName());
    System.out.println(child.getAbsolutePath());
}

He passes the tests and prints:

/var/folders/.../T/junit8666449860303204067/grandparent/parent/child
+6
source

TemporaryFolder has a method newFolder(String...folderNames)that allows you to create subdirectories.

tempFolder.newFolder("subFolderA", "subSubFolder")

http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html#newFolder(java.lang.String...)

0

Source: https://habr.com/ru/post/1653585/


All Articles