In my project Mockito.times(1)
is often used when verifying mocks:
verify(mock, times(1)).call();
This is redundant since Mockito uses implicit times(1)
for verify(Object)
, thus the following code does exactly what the code above does:
verify(mock).call();
So I'm going to write an a structural search drive inspection to report such cases (let's say, named something like Mockito.times(1) is redundant). As I'm not an expert in IntelliJ IDEA structural search, my first attempt was:
Mockito.times(1)
Obviously, this is not a good seach template because it ignores the call-site. Let's say, I find it useful for the following code and I would not like the inspection to trigger:
VerificationMode times = Mockito.times(1);
// ^ unwanted "Mockito.times(1) is redundant"
So now I would like to define the context where I would like the inspection to trigger. Now the inspection search template becomes:
Mockito.verify($mock$, Mockito.times(1))
Great! Now code like verify(mock, times(1)).call()
is reported fine (if times
was statically imported from org.mockito.Mockito
). But there is also one thing. Mockito.times
actually comes from its VerificationModeFactory
class where such verification modes are grouped, so the following line is ignored by the inspection:
verify(mockSupplier, VerificationModeFactory.times(1)).get();
My another attempt to fix this one was something like:
Mockito.verify($mock$, $times$(1))
where:
$mock$
is still a default template variable;$times$
is a variable with Text/regexp set totimes
, Whole words only and Value is read are set totrue
, andExpression type (regexp)
is set to(Times|VerificationMode)
-- at least this is the way I believed it should work.
Can't make it work. Why is Times
also included to the regexp? This is the real implementation of *.times(int)
, so, ideally, the following line should be reported too:
verify(mockSupplier, new Times(1)).get();
Of course, I could create all three inspection templates, but is it possible to create such a template using single search template and what am I missing when configuring the $times$
variable?
(I'm using IntelliJ IDEA Community Edition 2016.1.1)