如何在Intellij中进行结构搜索和替换

时间:2021-11-29 08:36:54

I am migrating my Junit tests from Junit v3 to Junit v4.

我正在将Junit测试从Junit v3迁移到Junit v4。

I was hoping to use the search and replace structurally "srs" to add an @Test annotation before all methods starting with test.* using my favorite IDE Intellij.

我希望使用搜索并替换结构上的“srs”来在使用我最喜欢的IDE Intellij开始的所有方法之前添加@Test注释。

I just cant figura out how to do it... I can create a query that will find all methods without an annotation (there is an example bundled in Intellij) but when used to do a replace either my class gets replaced by only the method names OR nothing is found.

我只是不知道怎么做...我可以创建一个查询,找到没有注释的所有方法(在Intellij中有一个示例捆绑)但是当用于替换时,我的类只被替换为方法名字或没有找到。

Maybe a regexp is easier :-)

也许正则表达式更容易:-)

/Konstantin

3 个解决方案

#1


It is not possible, as of now, but there are requests for class members replacements.

到目前为止,这是不可能的,但是要求更换班级成员。

Here is a useful link for getting started with ssr: https://www.jetbrains.com/idea/docs/ssr.pdf

以下是开始使用ssr的有用链接:https://www.jetbrains.com/idea/docs/ssr.pdf

#2


I tried with the following SSR but I have the same problem than you :

我尝试使用以下SSR,但我遇到的问题与你相同:

Search template :

搜索模板:

public class $TestCase$ extends $TestCaseClazz$ {
  public void $testMethod$();
}

Replacement template :

替换模板:

@Test public void $testMethod$();

And I check "This variable is target of the search" on testMethod. The preview seems ok, but IntelliJ actually delete the whole method :(

我在testMethod上检查“此变量是搜索的目标”。预览似乎没问题,但IntelliJ实际上删除了整个方法:(

#3


Open Search -> Replace in path
Set Directory to the directory where your test is placed
Set search string to: public void test.
Set replace string to: @Test\n\tpublic void test.
Check Regular Expression box.

Enjoy.

打开搜索 - >替换路径将目录设置为放置测试的目录将搜索字符串设置为:public void test。将替换字符串设置为:@Test \ n \ tpublic void test。选中正则表达式框。请享用。

#1


It is not possible, as of now, but there are requests for class members replacements.

到目前为止,这是不可能的,但是要求更换班级成员。

Here is a useful link for getting started with ssr: https://www.jetbrains.com/idea/docs/ssr.pdf

以下是开始使用ssr的有用链接:https://www.jetbrains.com/idea/docs/ssr.pdf

#2


I tried with the following SSR but I have the same problem than you :

我尝试使用以下SSR,但我遇到的问题与你相同:

Search template :

搜索模板:

public class $TestCase$ extends $TestCaseClazz$ {
  public void $testMethod$();
}

Replacement template :

替换模板:

@Test public void $testMethod$();

And I check "This variable is target of the search" on testMethod. The preview seems ok, but IntelliJ actually delete the whole method :(

我在testMethod上检查“此变量是搜索的目标”。预览似乎没问题,但IntelliJ实际上删除了整个方法:(

#3


Open Search -> Replace in path
Set Directory to the directory where your test is placed
Set search string to: public void test.
Set replace string to: @Test\n\tpublic void test.
Check Regular Expression box.

Enjoy.

打开搜索 - >替换路径将目录设置为放置测试的目录将搜索字符串设置为:public void test。将替换字符串设置为:@Test \ n \ tpublic void test。选中正则表达式框。请享用。