IntelliJ:自定义生成的测试方法的命名

时间:2017-07-13 07:11:07

标签: java intellij-idea junit

如果我为一个让我们说getId()方法的类生成测试,那么相应的测试方法也将被称为getId()

是否可以设置IntelliJ为自动生成的测试方法添加一些静态前缀?

例如:

getId() - > shouldGetId()

doStuff() - > shouldDoStuff()

2 个答案:

答案 0 :(得分:8)

转到viewDidLayoutSubView并选择Preferences > Editor > File and Code Templates。默认情况下,此格式为

JUnit4 Test Method

更改为

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

File and Code Templates

答案 1 :(得分:3)

您可以轻松更改测试方法的模板,如下所示:

第1步:

创建一个测试类并按下键盘上的ALTR + Insert按钮,您将看到一个弹出窗口,如下面的屏幕截图所示。然后按键盘上的右箭头键,将打开“编辑模板”菜单。然后点击它。

enter image description here

第2步:

根据需要更改方法模板并按下(在我的原因中它将被重定向到TestNG方法模板,但你的将是JUnit )。

enter image description here

第3步:

当您通过ALTR + Insert插入方法时(在第一步中显示),单击Test Method菜单,然后它将生成如下方法: enter image description here