Java TemporaryFolder getRoot () exception

I am trying to use org.junit.rules.TemporaryFolder in one of my junit to check file input / output. I initialized it as follows:

the code:

   @Rule
   public TemporaryFolder temporaryFolder;

   @Before 
    public void setup() {
       this.temporaryFolder = new TemporaryFolder();
    }

   @After
   public void tearDown() {}

   @Test
   public void testCsvDataFile() throws IOException {
       File testCsvFile = this.temporaryFolder.newFile("text.csv");
       FileWriter csvFileWriter = new FileWriter(testCsvFile);
       BufferedWriter bufferedWriter = new BufferedWriter(csvFileWriter);
       bufferedWriter.write("col1,col2,col3\n");
       bufferedWriter.write("1,test1,val1\n");
       bufferedWriter.write("2,test2,val2\n");
       bufferedWriter.close();
       Map<Long,Data> data = MyReader.readCSV(testCsvFile);
       assertTrue(2 == data.size());
   }

However, I get an exception:

An exception:

java.lang.IllegalStateException: the temporary folder has not yet been created
at org.junit.rules.TemporaryFolder.getRoot(TemporaryFolder.java:127)
at org.junit.rules.TemporaryFolder.newFile(TemporaryFolder.java:64)

When I look at the TemporaryFolder code, it uses the internal attribute folder in the getRoot () function, which is never set. The constructor sets another field: parentFolder.

There is a create () method that sets a folder variable, but is marked for testing purposes.

I am using JDK 1.7. Am I creating a TemporaryFolder incorrectly? Is there anything else, a system property that needs to be set for this?

+4
source share
1 answer

setup(), :

   @Rule
   public TemporaryFolder temporaryFolder = new TemporaryFolder();

   @Before 
   public void setup() {...}

   @After
   public void tearDown() {...}
+7

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


All Articles