Use property user.home in java tests

Hello,

I’m trying to test an export function in my code, and I’m using for that the following Java property:
System.getProperty(“user.home”)
This leads me to /home/runner.

Before running any test, I run the following commands:
mkdir /home/runner/BooksExports
chmod 777 -R /home/runner/BooksExports
This one works perfectly.

But when the test runs, I get the following error:

java.io.FileNotFoundException: /home/runner\BooksExports\Toto.docx (Permission denied)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.(FileOutputStream.java:187)
at com.jher.nid_aux_histoires.export.ExportDocx.buildWordFile(ExportDocx.java:134)
at com.jher.nid_aux_histoires.export.ExportDocx.launchGeneration(ExportDocx.java:70)
at com.jher.nid_aux_histoires.service.impl.ExportServiceImpl.export(ExportServiceImpl.java:89)
at com.jher.nid_aux_histoires.service.impl.ExportServiceImpl.lambda$exportBook$0(ExportServiceImpl.java:60)
at java.base/java.lang.Thread.run(Thread.java:829)

This is the code where I get the error:

		File outputFile = new File(outputFilePath);
		try (OutputStream out = new FileOutputStream(outputFile)) { // error occurs here
			[...]
		} catch (IOException e) {
			[...]
		}

I can’t figure out why I’m not able to create this file. Maybe I just can’t do that because it’s not allowed…
Does anyone has an idea ?

Thanks

Looks like your test is trying to create a file named runner\BooksExports\Toto.docx in /home/. Note that the backslashes aren’t directory separators on Linux! :wink:

Thanks for your answer ! You’re right, I had to update my folder separators.

1 Like