Resource files not found from JUnit test cases

前端 未结 6 2097
无人及你
无人及你 2020-12-24 04:45

Summary

My JUnit tests are not finding the files they require during execution. I\'m using Maven for dependency management and compilation.

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 05:05

    The test Resource files(src/test/resources) are loaded to target/test-classes sub folder. So we can use the below code to load the test resource files.

    String resource = "sample.txt";
    File file = new File(getClass().getClassLoader().getResource(resource).getFile());
    
    System.out.println(file.getAbsolutePath());
    

    Note : Here the sample.txt file should be placed under src/test/resources folder.

    For more details refer options_to_load_test_resources

提交回复
热议问题