Resource files not found from JUnit test cases

前端 未结 6 2073
无人及你
无人及你 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:00

    Main classes should be under src/main/java
    and
    test classes should be under src/test/java

    If all in the correct places and still main classes are not accessible then
    Right click project => Maven => Update Project
    Hope so this will resolve the issue

    0 讨论(0)
  • 2020-12-24 05:02

    This is actually redundant except in cases where you want to override the defaults. All of these settings are implied defaults.

    You can verify that by checking your effective POM using this command

    mvn help:effective-pom

        <finalName>name</finalName>
        <directory>target</directory>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
    

    For example, if i want to point to a different test resource path or resource path you should use this otherwise you don't.

        <resources>
            <resource>
                <directory>/home/josh/desktop/app_resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>/home/josh/desktop/test_resources</directory>
            </testResource>
        </testResources>
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-24 05:07

    Make 'maven.test.skip' as false in pom file, while building project test reource will come under test-classes.

    <maven.test.skip>false</maven.test.skip>

    0 讨论(0)
  • 2020-12-24 05:15

    You know that Maven is based on the Convention over Configuration pardigm? so you shouldn't configure things which are the defaults.

    All that stuff represents the default in Maven. So best practice is don't define it it's already done.

        <directory>target</directory>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
    
    0 讨论(0)
  • 2020-12-24 05:19

    My mistake, the resource files WERE actually copied to target/test-classes. The problem seemed to be due to spaces in my project name, e.g. Project%20Name.

    I'm now loading the file as follows and it works:

    org.apache.commons.io.FileUtils.toFile(myClass().getResource("resourceFile.txt")‌​);
    

    Or, (taken from Java: how to get a File from an escaped URL?) this may be better (no dependency on Apache Commons):

    myClass().getResource("resourceFile.txt")‌​.toURI();
    
    0 讨论(0)
提交回复
热议问题