Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 571
tbruyelle Avatar asked Apr 12 '11 15:04

tbruyelle


People also ask

How do I find the relative path in IntelliJ?

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.

How can JUnit be implemented using Maven?

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.


1 Answers

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.

like image 157
tbruyelle Avatar answered Oct 13 '22 12:10

tbruyelle