- Summarize the problem:
I'm testing a Spring-Rest/MVC-Api (without Spring-Boot) with JUnit 5.
I have a class CatRepositoryBackUpRepo, i read and write a file there that i link with
private File datei = new File("Cats.json");
. This file is in src/main/resource. This works fine when i run the program. I checked it with the println-Logging-Text.
When i want to test this read-method in a Junit-test-Class (link at the end), it didn't find the file, and throw a file not found exception.
- Describe what you’ve tried:
I created a /src/test/resource-folder and place the same file there, but didnt worked. Then i used the test-class to write such a file and it was placed on the same level as the /src-Folder and the pom.xml, on the project-path. It seems that the property for the resource-path when running the testclass is the problem, but i dont know how to fix it, how to tell the class-under-test the correct property.
I run the test-cases with Junit in a IntelliJ Ultimate Edit., the working directory of the run-configuration for Junit is set to $MODULE_WORKING_DIR$
.
- Add some code
This is the class under test, with the method under test readCats()
https://github.com/IngmarEckhardt/RestControl/blob/master/src/main/java/de/cats/restcat/service/CatRepositoryBackupRepo.java
And this is the test-class, the only method that doesnt worked is readCats_withHDDready_shouldReturnArrayList()