I have a maven project with a module
/myProject pom.xml /myModule pom.xml /foo bar.txt
Consider a Junit in myModule which needs to open bar.txt, with maven the basedir is the module directory.
So to open the file bar.txt :
new File("foo/bar.txt")
This works well when you execute mvn test
BUT when you launch the same junit in intellij, it fails because Intellij sets the basedir in the project directory, not the module directory.
Intellij tries to open myProject/foo/bar.txt
instead of myProject/myModule/foo/bar.txt
Is there a way to deal with that ?
If you have a multi-project setup, it may not be obvious what the "root" directory is for a relative path. In that case, open the Terminal tab in your IntelliJ and see what directory it lands in. That is the "root" directory. Your relative path should start there.
The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With