Editor => File and Code Templates => Modifying DIR_PATH to create files in different folders
Answered
In many projects, it is common to have unit tests.
These unit tests are normally located in a different folder.
Here is an example:
What I'm trying to do is to tell IntelliJ IDEA that this particular file, should go into a folder with similar package name but inside the `test` folder:
When I run this, the unit test file isn't created where I was expecting it to be created.
I was expecting the file `ExpMemTest` to be created in the folder:
src/test/java/com/example/demo/experiment
How can I make this work out? Should I create a plugin of some shorts?
Is there a future feature ticket that I should try to create to make this happen?
Please sign in to leave a comment.
Hello,
Please clarify what is the workflow of creating the test class in your case?
Does it work for you if you generate the test class like following:
I mean that the test class should be created in the corresponding package under the source root in this scenario.
Thank you
Olga, thank you for taking care of this issue I'm pointing out.
What I'm asking is a way to decide where each individual files are going to be created.
In the case of the unit test, even do the feature shown in the image creates the unit test, it doesn't create the unit test with the code I want it to include.
Also, there could be other cases, where I wish different files to be located in different places.
For example, if I work with Grails or Ruby, the locations of the files are very important.
Depending of the locations where the files are located is that the framework will do the extra work needed to make it work.
To make it more clear, I would like to provide some examples.
In this example, I'm trying to tell IntelliJ that the file, with my own content, should be created inside the folder test, following the same package:
${DIR_PATH.replace("main", "test")}/${NAME}Test
In this example, I created something that doesn't currently exist: ROOT_PROJECT
It would be great to have something like ${ROOT_PROJECT} because I could create the files I need in different locations throughout the project:
${ROOT_PROJECT}/src/controller/${NAME}Controller, if I would be creating a controller inside the controller folder.
${ROOT_PROJECT}/src/service/${NAME}Service, if I would be creating a controller inside the controller folder.
${ROOT_PROJECT}/src/service/${NAME}Repository, if I would be creating a controller inside the repository folder.
The current feature doesn't allow for this, every single file you create is only created in the package or older you are in.
Having a little bit more control where the files should be created would be wonderful.
Hello,
Unfortunately there is no the tool to do it right away. Please follow the issue created for this:
https://youtrack.jetbrains.com/issue/IDEA-280905
Working with Dart I don't have support for creating tests as easily as I do with Java.
So I decided to implement my own File Template myself and I encountered this same problem.
In my tests I realized that the variable ${DIR_PATH} is only set after the creation of the file, that then the template is processed and written to the file.
I don't know if this will work for you, for Dart I worked on the variable ${PACKAGE_NAME}
The template for the filename looks like this:
#if($PACKAGE_NAME)../$PACKAGE_NAME.replaceAll('\.','/').replaceAll('[\w_]{1,}','..')/test/$PACKAGE_NAME.replaceAll('\.','/')/#end$NAME.replaceAll(' ','_').toLowerCase()_testIn my tests it generated the file in place as expected.
Marcusedu I will give it a try to what you did to see if works for me thank you.
in an android studio proiect context Marcusedu's solution works if you add another ../ in front of the package manipulation
#if($PACKAGE_NAME)../../$PACKAGE_NAME.replaceAll('\.','/').replaceAll('[\w_]{1,}','..')/test/$PACKAGE_NAME.replaceAll('\.','/')/#end$NAMETest
thanks a lot!