IntelliJ can't find classpath test resource

后端 未结 7 840
你的背包
你的背包 2020-12-02 14:18

I\'m having a problem where IntelliJ 13.1.4, when running a unit test, can\'t find a ServiceLoader file in the src/test/resources directory of my m

相关标签:
7条回答
  • 2020-12-02 14:35

    I managed to get it working better by creating a test-resources directory parallel to the test directory (!).

    Still seeing intermittent problems with this, though. Those times, it's possible to open the IntelliJ project settings and define the test resources as source instead (the Gradle project will remain unchanged).

    Must be an IntelliJ bug, as I've experienced the same thing in Android Studio.

    EDIT:

    I found a workaround to the issue.

    Add the following at the end of the Gradle config (and specify your test resources path):

    task copyTestResources(type: Copy) {
        from "${projectDir}/src/test/resources"
        into "${buildDir}/classes/test"
    }
    processTestResources.dependsOn copyTestResources
    
    0 讨论(0)
  • 2020-12-02 14:38

    If your project is multi maven and other answers did not work, try this one.

    Change the working directory in intellij. Right Click, go to edit configuration, select the required maven project directory you want to run.

    enter image description here

    0 讨论(0)
  • 2020-12-02 14:42

    When there is a multi-module project, IntelliJ tends to set the top module as the working directory. If this this the case, one can try changing it to the sub-module directory.

    0 讨论(0)
  • 2020-12-02 14:46

    In my case, I simply ran the tests through maven command line, i.e. a basic mvn clean install. After that it worked in IDEA.

    0 讨论(0)
  • 2020-12-02 14:51

    I face a similar issue with IntelliJ 14. Despite having the necessary files in my resources folder and ensuring that the folder is specified as a resource in the module settings, IntelliJ intermittently fails to load my files onto the classpath.

    One workaround is to add the required folder as a dependency for the module in the module settings.

    First, click on the "+" button to add a new dependency.

    Then, select the folder you wish to add to the classpath.

    Then select the "classes" option.

    Once that is done, re-build your module once and hopefully that should do the trick.

    Ideally this should not at all be an issue. All files in the resource folder should directly be put in the classpath. However, every IDE has it's quirks.

    0 讨论(0)
  • 2020-12-02 14:53

    I ran into the same issue, where Junit was failing(FileNotFound) because I had space in my root project folder. eg: my project/project1/....

    • I removed the space from the root folder, my_project/project1/ all test passed properly.
    0 讨论(0)
提交回复
热议问题