IDEA Structural Search - matching code at end of method

194 views Asked by At

I'm trying to find methods like this:

@Test
public void testStuff()
{
    doStuff();
    doOtherStuff();

    mockery.assertIsSatisfied();
}

The goal is to remove the mockery.assertIsSatisfied(). I can't just remove all calls to it because sometimes it's in the middle of a method or at the end of a loop, where it seems reasonable. Of course, we're using the JUnit @Rule to invoke this automatically, so it's redundant to have it at the end of every test.

I thought this would be the right template:

@Test
public void $testMethod$() throws Exception
{
    $Statements$;    // configured this as 1..many
    mockery.assertIsSatisfied();
}

This matches about 2 methods out of the 400+ usages of that method. Randomly picking some of the other usages of that method, I see that others should have matched the pattern too. (I can't figure out what is common between the ones which do match. They're both try blocks, but so are some of the ones which don't match.)

So what is the right way to do this?

Edit: I just noticed I had hard-coded a throws Exception onto this one, so I re-executed the search without it, and that gives 0 results. In case anybody is wondering.

1

There are 1 answers

1
Bas Leijdekkers On

It's (currently) not possible to use a naked method as a pattern. To search for a method you need to surround it with a class like this:

class $Class$ {
    @Test
    public void $testMethod$() // 1..unlimited
    {
        $Statements$;    // 1..unlimited
        mockery.assertIsSatisfied();
    }
}

Be sure that the method is also configured with occurrences 1..unlimited, or only one method per class will be found.