fxr
fxr

Reputation: 71

How to automatically replace Mockito thenReturn with Mockito doReturn

Mockito offers two ways to return certain values: the thenReturn and the doReturn syntax. The differences between the two are already documented here on stackoverflow. I'm currently working on a Java class where the two syntaxes are mixed. I'm looking for an automated way to transform thenReturn to doReturn.

Upvotes: 1

Views: 491

Answers (1)

fxr
fxr

Reputation: 71

This can be done in IntelliJ via the custom structural search inspection:

<replaceConfiguration name="when" text="when($mock$.$MethodCall$($Parameter$)).thenReturn($result$)" recursive="false" caseInsensitive="false" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="doReturn($result$).when($mock$).$MethodCall$($Parameter$)">
  <constraint name="__context__" within="" contains="" />
  <constraint name="mock" within="" contains="" />
  <constraint name="result" within="" contains="" />
  <constraint name="MethodCall" within="" contains="" />
  <constraint name="Parameter" minCount="0" maxCount="2147483647" within="" contains="" />
</replaceConfiguration>

You can just import this template from clipboard and save the custom inspection. Then you can run it via "Run inspection by name => structural search".

Upvotes: 4

Related Questions