How to deal with relative path in Junits between Maven and Intellij

末鹿安然 提交于 2019-12-02 16:43:31

If you want to keep your code, you can try to change the working directory in the run/debug configuration (first entry in the combo box giving access to what you want to run) Set this to your module root. But prefer the other suggested approach:

ClassLoader.getSystemResourceAsStream(youPath) 

Or my preferred:

getClass.getResource(youPath)

or

getClass.getResourceAsStream(youPath)

A leading '/' in path indicates the working dir of your project, while no '/' indicates a relative dir to current class.

I use this last solution for my tests: I put my test data resources at the same package level as the test source, or in a subdir to avoid too messy package.

This way I can do a simple call without complicated path and without having to deal with working directory:

project-root  
  - module A  
    - src  
      - test
        - rootfile.txt  
        - my-complicated-package-naming-root
          - mypackage
            - Test.java
            - testResource.xml  

I can get the files this way:

final URL rootfile= Test.class.getResource("/rootfile.txt");
final URL testResource= Test.class.getResource("testResource.xml");

Solution inspired by Guillaume :

In Run->Edit configuration->Defaults->JUnit->Working directory set the value $MODULE_DIR$ and Intellij will set the relative path in all junits just like Maven.

a) Don't use Files, use InputStreams. get your InputStream via

ClassLoader.getSystemResourceAsStream("foo/bar.xml")

Most APIs that deal with Files are happy with InputStreams as well.

b) Don't use foo directories, use directories both maven and your IDE know about (i.e. put them in src/main/resources or src/test/resources, so they are on the Class Path)

c) If you have an API that absolutely needs a File, not an InputStream, you can still do

new File(ClassLoader.getSystemResource("foo/bar.xml").toURI())

You can specify the working directory for the test runner in the Run/Debug Configurations window:

Solution from @tbruyelle works if you keep your project files(.idea) in the same directory as the source code. If you choose Keep Project files in ... in a different location, then $MODULE_DIR$ is trying to look up in the workspace directory and the paths cannot be found. This looks like a bug on IntelliJ, hope they fix it soon.

Workaround: You can specify absolute / relative path of the maven module in working directory

$MODULE_DIR$/../master/mavenmodule1

$MODULE_DIR$: points to workspace directory
../: relative path to source code
master: root directory of your source code
mavenmodule1: maven module name / directory name of the child module.

For multi module maven project you don't have a choice, you need to have a different run configurations pointing to that module. I wish there is another variable that points just to the $MAVEN_MODULE$ ($MODULE_DIR$/../master/$MAVEN_MODULE$) so we can use this configuration for all modules. For the above example, $MAVEN_MODULE$ will be substituted with mavenmodule1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!