3

I want to perform structural search on my tests to detect all methods that look like tests but are not annotated with @Test.

I tried with this pattern with no success (no matching code found):

@$Annotation$ public void $method$()

$Annotation$ - text like: ^Test$, min occurs: 0, max occurs: 0
$method$ - text like: should|test, min occurs: 1, max occurs: 1

What am I doing wrong?

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103

2 Answers2

2

Seems, you have to try to provide a class with your pattern too, as it's done in templates:

class $Class$ {
  @$Annotation$( )
  $MethodType$ $MethodName$($ParameterType$ $ParameterName$);
}

Furthermore, you have to change the $method$ variable to something like should.*|test.* to accept not a whole words only.

I've tested it with IntelliJ Idea 15, it's working for the methods like:

public void testSomething() {

}

public void shouldSomething() {

}

only if the $Class$ is provided in search template and the method names regexes contain .* to match any method, which name begins with test or should.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 1
    Method patterns without surrounding class will be possible in the next version of IntelliJ IDEA. – Bas Leijdekkers Nov 12 '15 at 12:38
  • Thanks, it works, but I had to replace `$MethodType$` with `public void` as it was matching also `private` methods – Michal Kordas Nov 12 '15 at 13:14
  • @MichalKordas you're welcome. Yes, I just inserted this one as an example, how it's done by default. While tested it, I used the same pattern, as you provided, but with the class – Stanislav Nov 12 '15 at 15:10
0

You could also try the Old style JUnit test method in JUnit 4 class inspection. This inspection detects methods that look like tests, but are not annotated with @Test.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68