Monday, February 13, 2017

Creating a Test Template in IntelliJ Idea


Here is a quick guide for a test template in intellij. Almost all of the time i am using maven for the java projects i work on and junit and assertj is my primary testing tools. Every time i create test classes i am typing the static includes and a test method annotated with test. Although it's not that much boiler plate this can be prevented thanks to intellij file templates.

We want a test class to be generated automatically for that we are going to create a file template with name "JavaTest" (you can pick any name you want). Here is a quick step by step guide for that:

1. Create a file that contains the things that you want to be part of the template as a java file. (For this case i want junit and assertj static imports to be present along with junit import and a test method annotated with @Test)



2. Select Tools -> Save File As Template from the top menu. This will bring up the Save File As Template Dialog. Give the name "JavaTest" and save the template. (you can change the template as you please)



3.  Right Click to the package you want to create a unit test. From the New menu find the name "JavaTest" and create the file... Voila there you have your tiny little cute test file ready to use :)

As a long term Netbeans user, i find intelliJ easier and faster to use but still from the bottom of my heart i am waiting for Netbeans to make a come back ...