通过 IntelliJ 2019 的 "Create test" 功能重新定义用于创建 JUnit 5 class 的模板

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

菜单项Code>Generate…>Test…显示此对话框。

…并在结果测试中生成这样的方法 class:

@Test
void fromDuration () {
}

我希望为每个正在生成的测试方法自动插入 @DisplayName 注释(JUnit 5 中的新注释)。像这样:

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

➥ 有没有办法改变 IntelliJ 生成测试的方式来包含 @DisplayName 注释?

我找了一些模板来编辑,但没找到。

JUnit 5 方法模板可以在 Settings 中配置(Preferences on Mac) | Editor | File and Code Templates | Code 选项卡,JUnit 5 测试方法

改变这个:

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

…为此:

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

创建一个新的模板组,然后启用模板

@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("$TEST_NAME$")
void $METHOD_NAMES$(){
$END$
$BODY$
}

键入测试并在 java class

上查看模板呈现