Home » Java » java – Loading an actual Folder in org.junit.rules.TemporaryFolder-Exceptionshub

java – Loading an actual Folder in org.junit.rules.TemporaryFolder-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Using the command: def tempFile = BlockNoEmptyTest.class.getResource("src/valid-block.scss"), I can get a file from the resource folder belonging to the package of the class BlockNoEmptyTest. Further, the following code snippet works:

import spock.lang.Specification;
import org.junit.rules.TemporaryFolder
import org.junit.Rule

class BlockNoEmptyTest extends Specification {
  @Rule TemporaryFolder testProjectDir = new TemporaryFolder()
  File settingsFile
  File buildFile

  def setup() {
    settingsFile = testProjectDir.newFile('settings.gradle')
    buildFile = testProjectDir.newFile('build.gradle')
  }

  def temp() {
    settingsFile << "rootProject.name = 'hello-world'"
    buildFile << BlockNoEmptyTest.class.getResource("build.txt").getText("UTF-8")

    // BlockNoEmptyTest.cass.getResource("src/").... # how to easily load the whole src folder
  }
}

I have, in my resource folder, a src folder that I would like to load in my TemporaryFolder. It contains many files and multiple subdirectories. What is a good way to achieve this?

How to&Answers: