[英]How to add static imports to IntelliJ IDEA live template
我需要將以下 Eclipse 模板移植到 IntelliJ IDEA
/**
* See method name.
*/
@${testType:newType(org.junit.Test)}
public void should${testdesc}() {
// given ${cursor}
${staticImport:importStatic('org.hamcrest.Matchers.*', 'org.junit.Assert.*', 'org.mockito.BDDMockito.*')}
// when
// then
}
到目前為止我所擁有的是
/**
* See method name.
*/
@org.junit.Test
public void should$EXPR$() {
// given $END$
${staticImport:importStatic('org.hamcrest.Matchers.*', 'org.junit.Assert.*', 'org.mockito.BDDMockito.*')}
// when
// then
}
然后勾選Shorten FQ names
標志。
${staticImport:importStatic()}
表達式的 IDEA 等價物是什么?
您不能只在實時模板中導入靜態導入。 (您可以為文件模板,見下文)。 但是您可以在模板中使用方法時。 您只需完全限定該類,然后選擇“縮短 FQ 名稱”和“如果可能使用靜態導入”選項。 例如,以下內容:
org.junit.Assert.assertEquals("$END$", $EXPECTED$, $ACTUAL$);
會導致:
import static org.junit.Assert.*;
. . .
assertEquals("my error message", myExpectedVar, myActualVar);
調用時。 (我將$EXPECTED$
和$ACTUAL$
變量設置為variableOfType("")
與相應的默認值expected
和actual
)
如果您希望將某些靜態導入包含在所有單元測試中,那么我建議您編輯“類”文件和代碼模板。 例如:
package ${PACKAGE_NAME};
#if ($NAME.endsWith("Test"))
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.BDDMockito.*;
#end
#parse("File Header.java")
public class ${NAME}
{
#if ($NAME.endsWith("Test"))
// Add any default test methods or such you want here.
#end
}
但是請記住,如果您打開了“即時優化導入”選項(在 IDE 設置 > 編輯器 > 自動導入中),靜態導入將立即被刪除,除非您還包含一個方法(或其他代碼)使用靜態導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.