2929import java .util .ArrayList ;
3030import java .util .List ;
3131
32+ import static org .openrewrite .java .testing .jmockit .JMockitBlockType .NonStrictExpectations ;
3233import static org .openrewrite .java .testing .jmockit .JMockitBlockType .Verifications ;
3334
3435class JMockitBlockRewriter {
3536
3637 private static final String WHEN_TEMPLATE_PREFIX = "when(#{any()})." ;
38+ private static final String VERIFY_TEMPLATE_PREFIX = "verify(#{any()}" ;
39+ private static final String LENIENT_TEMPLATE_PREFIX = "lenient()." ;
40+
3741 private static final String RETURN_TEMPLATE_PREFIX = "thenReturn(" ;
3842 private static final String THROW_TEMPLATE_PREFIX = "thenThrow(" ;
3943 private static final String LITERAL_TEMPLATE_FIELD = "#{}" ;
@@ -131,6 +135,11 @@ private void rewriteMethodInvocation(List<Statement> statementsToRewrite) {
131135 rewriteResult (invocation , mockInvocationResults .getResults ());
132136 }
133137
138+ if (blockType == NonStrictExpectations ) {
139+ // no verify for NonStrictExpectations
140+ return ;
141+ }
142+
134143 boolean hasTimes = false ;
135144 if (mockInvocationResults .getTimes () != null ) {
136145 hasTimes = true ;
@@ -145,22 +154,19 @@ private void rewriteMethodInvocation(List<Statement> statementsToRewrite) {
145154 rewriteVerify (invocation , mockInvocationResults .getMaxTimes (), "atMost" );
146155 }
147156 if (!hasResults && !hasTimes ) {
148- rewriteVerify (invocation , null , null );
157+ rewriteVerify (invocation , null , "" );
149158 }
150159 }
151160
152161 private void removeBlock () {
153162 methodBody = JavaTemplate .builder ("" )
154163 .javaParser (JavaParser .fromJavaVersion ())
155164 .build ()
156- .apply (
157- new Cursor (visitor .getCursor (), methodBody ),
158- nextStatementCoordinates
159- );
165+ .apply (new Cursor (visitor .getCursor (), methodBody ), nextStatementCoordinates );
160166 if (bodyStatementIndex == 0 ) {
161167 nextStatementCoordinates = methodBody .getCoordinates ().firstStatement ();
162168 } else {
163- setNextCoordinatesAfterLastStatementAdded (0 );
169+ setNextStatementCoordinates (0 );
164170 }
165171 }
166172
@@ -171,17 +177,26 @@ private void rewriteResult(J.MethodInvocation invocation, List<Expression> resul
171177 rewriteFailed = true ;
172178 return ;
173179 }
174- visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "when" );
175180
176181 List <Object > templateParams = new ArrayList <>();
177182 templateParams .add (invocation );
178183 templateParams .addAll (results );
179-
180- methodBody = rewriteTemplate (template , templateParams , nextStatementCoordinates );
181- setNextCoordinatesAfterLastStatementAdded (++numStatementsAdded );
184+ this .rewriteFailed = !rewriteTemplate (template , templateParams , nextStatementCoordinates );
185+ if (!this .rewriteFailed ) {
186+ this .rewriteFailed = true ;
187+ setNextStatementCoordinates (++numStatementsAdded );
188+ // do this last making sure rewrite worked and specify hasReference=false because framework cannot find static
189+ // reference for when method invocation when lenient is added.
190+ boolean hasReferencesForWhen = true ;
191+ if (this .blockType == NonStrictExpectations ) {
192+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "lenient" );
193+ hasReferencesForWhen = false ;
194+ }
195+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "when" , hasReferencesForWhen );
196+ }
182197 }
183198
184- private void rewriteVerify (J .MethodInvocation invocation , @ Nullable Expression times , @ Nullable String verificationMode ) {
199+ private void rewriteVerify (J .MethodInvocation invocation , @ Nullable Expression times , String verificationMode ) {
185200 if (invocation .getSelect () == null ) {
186201 // cannot write a verification statement for an invocation without a select field
187202 return ;
@@ -194,7 +209,6 @@ private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression t
194209 }
195210 templateParams .add (invocation .getName ().getSimpleName ());
196211 String verifyTemplate = getVerifyTemplate (invocation .getArguments (), verificationMode , templateParams );
197-
198212 JavaCoordinates verifyCoordinates ;
199213 if (this .blockType == Verifications ) {
200214 // for Verifications, replace the Verifications block
@@ -203,21 +217,22 @@ private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression t
203217 // for Expectations put the verify at the end of the method
204218 verifyCoordinates = methodBody .getCoordinates ().lastStatement ();
205219 }
220+ this .rewriteFailed = !rewriteTemplate (verifyTemplate , templateParams , verifyCoordinates );
221+ if (!this .rewriteFailed ) {
222+ if (this .blockType == Verifications ) {
223+ setNextStatementCoordinates (++numStatementsAdded ); // for Expectations, verify statements added to end of method
224+ }
206225
207- methodBody = rewriteTemplate (verifyTemplate , templateParams , verifyCoordinates );
208- if (this .blockType == Verifications ) {
209- setNextCoordinatesAfterLastStatementAdded (++numStatementsAdded );
210- }
211-
212- // do this last making sure rewrite worked and specify hasReference=false because in verify case it cannot find
213- // the static reference in AddImport class, and getSelect() returns not null
214- visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "verify" , false );
215- if (verificationMode != null ) {
216- visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , verificationMode );
226+ // do this last making sure rewrite worked and specify hasReference=false because in verify case framework
227+ // cannot find the static reference
228+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "verify" , false );
229+ if (!verificationMode .isEmpty ()) {
230+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , verificationMode );
231+ }
217232 }
218233 }
219234
220- private void setNextCoordinatesAfterLastStatementAdded (int numStatementsAdded ) {
235+ private void setNextStatementCoordinates (int numStatementsAdded ) {
221236 // the next statement coordinates are directly after the most recently written statement, calculated by
222237 // subtracting the removed jmockit block
223238 int nextStatementIdx = bodyStatementIndex + numStatementsAdded - 1 ;
@@ -228,23 +243,28 @@ private void setNextCoordinatesAfterLastStatementAdded(int numStatementsAdded) {
228243 }
229244 }
230245
231- private J . Block rewriteTemplate (String verifyTemplate , List <Object > templateParams , JavaCoordinates
246+ private boolean rewriteTemplate (String template , List <Object > templateParams , JavaCoordinates
232247 rewriteCoords ) {
233- JavaTemplate .Builder builder = JavaTemplate .builder (verifyTemplate )
248+ int numStatementsBefore = methodBody .getStatements ().size ();
249+ methodBody = JavaTemplate .builder (template )
234250 .javaParser (JavaParser .fromJavaVersion ().classpathFromResources (ctx , "mockito-core-3.12" ))
235- .staticImports ("org.mockito.Mockito.*" );
236- return builder
251+ .staticImports ("org.mockito.Mockito.*" )
237252 .build ()
238253 .apply (
239254 new Cursor (visitor .getCursor (), methodBody ),
240255 rewriteCoords ,
241256 templateParams .toArray ()
242257 );
258+ return methodBody .getStatements ().size () > numStatementsBefore ;
243259 }
244260
245- private static @ Nullable String getWhenTemplate (List <Expression > results ) {
261+ private @ Nullable String getWhenTemplate (List <Expression > results ) {
246262 boolean buildingResults = false ;
247- final StringBuilder templateBuilder = new StringBuilder (WHEN_TEMPLATE_PREFIX );
263+ StringBuilder templateBuilder = new StringBuilder ();
264+ if (this .blockType == NonStrictExpectations ) {
265+ templateBuilder .append (LENIENT_TEMPLATE_PREFIX );
266+ }
267+ templateBuilder .append (WHEN_TEMPLATE_PREFIX );
248268 for (Expression result : results ) {
249269 JavaType resultType = result .getType ();
250270 if (result instanceof J .Literal ) {
@@ -284,10 +304,9 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil
284304 templateBuilder .append (templateField );
285305 }
286306
287- private static String getVerifyTemplate (List <Expression > arguments , @ Nullable String
288- verificationMode , List <Object > templateParams ) {
289- StringBuilder templateBuilder = new StringBuilder ("verify(#{any()}" ); // eg verify(object
290- if (verificationMode != null ) {
307+ private static String getVerifyTemplate (List <Expression > arguments , String verificationMode , List <Object > templateParams ) {
308+ StringBuilder templateBuilder = new StringBuilder (VERIFY_TEMPLATE_PREFIX ); // eg verify(object
309+ if (!verificationMode .isEmpty ()) {
291310 templateBuilder .append (", " ).append (verificationMode ).append ("(#{any(int)})" ); // eg verify(object, times(2)
292311 }
293312 templateBuilder .append (").#{}(" ); // eg verify(object, times(2)).method(
0 commit comments