Search code examples
templatesintellij-ideajunitjunit5junit-jupiter

Redefine template used to create a JUnit 5 class via "Create test" feature of IntelliJ 2019


The menu item Code > Generate… > Test… displays this dialog box.

enter image description here

…and produces a method like this in the resulting test class:

@Test
void fromDuration () {
}

I want the @DisplayName annotation (new in JUnit 5) to be automatically inserted as well, for each test method being generated. Like this:

@Test
@DisplayName( "NameGoesHere" )
void fromDuration () {
}

➥ Is there a way to change the way IntelliJ generates the test to include the @DisplayName annotation?

I looked for some template to edit, but could not find one.


Solution

  • JUnit 5 method template can be configured in Settings (Preferences on Mac) | Editor | File and Code Templates | Code tab, JUnit 5 Test Method:

    method template

    Change this:

    @org.junit.jupiter.api.Test
    void ${NAME}() {
      ${BODY}
    }
    

    …to this:

    @org.junit.jupiter.api.Test
    @org.junit.jupiter.api.DisplayName ( "NameGoesHere" )
    void ${NAME}() {
      ${BODY}
    }