Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: customize naming of generated test methods

If I generate test for a class which has let's say getId() method the corresponding test method will be called getId() as well.

Is it possible to setup IntelliJ to add some static prefix for automatically generated test methods?

For instance:

getId() -> shouldGetId()

doStuff()-> shouldDoStuff()

like image 750
Sasha Shpota Avatar asked Dec 10 '22 10:12

Sasha Shpota


2 Answers

Go to Preferences > Editor > File and Code Templates and choose JUnit4 Test Method. This by default has the form of

@org.junit.Test
public void test${NAME}() {
  ${BODY}
}

Change to

@org.junit.Test
public void should${NAME}() {
  ${BODY}
}

File and Code Templates

like image 66
Adam Michalik Avatar answered Dec 21 '22 00:12

Adam Michalik


You can change the template of the test method easily as follows:

Step 1:

Create a test class and press ALTR + Insert button in the keyboard and you will see a popup as shown in the following screenshot. Then press the right arrow key on the keyboard and "Edit Template" menu will be opened. Then click on it.

enter image description here

Step 2:

Change the method template as required and press on (In my cause it will be redirected to TestNG method template, but yours will be JUnit).

enter image description here

Step 3:

When you insert a method by ALTR + Insert (Shown in the first step) click on Test Method menu, then it will generate a method as follows: enter image description here

like image 41
Hiran Perera Avatar answered Dec 20 '22 22:12

Hiran Perera