@@ -75,7 +75,7 @@ boolean isRewriteFailed() {
7575 this .newExpectations = newExpectations ;
7676 this .bodyStatementIndex = bodyStatementIndex ;
7777 this .blockType = blockType ;
78- nextStatementCoordinates = newExpectations .getCoordinates ().replace ();
78+ this . nextStatementCoordinates = newExpectations .getCoordinates ().replace ();
7979 }
8080
8181 J .Block rewriteMethodBody () {
@@ -125,74 +125,63 @@ private void rewriteMethodInvocation(List<Statement> statementsToRewrite) {
125125 final MockInvocationResults mockInvocationResults = buildMockInvocationResults (statementsToRewrite );
126126 if (mockInvocationResults == null ) {
127127 // invalid block, cannot rewrite
128- rewriteFailed = true ;
128+ this . rewriteFailed = true ;
129129 return ;
130130 }
131131
132132 J .MethodInvocation invocation = (J .MethodInvocation ) statementsToRewrite .get (0 );
133133 boolean hasResults = !mockInvocationResults .getResults ().isEmpty ();
134+ boolean hasTimes = mockInvocationResults .hasAnyTimes ();
134135 if (hasResults ) {
135- rewriteResult (invocation , mockInvocationResults .getResults ());
136+ rewriteResult (invocation , mockInvocationResults .getResults (), hasTimes );
136137 }
137138
138- if (blockType == NonStrictExpectations ) {
139- // no verify for NonStrictExpectations
139+ if (! hasResults && ! hasTimes && ( this . blockType == JMockitBlockType . Expectations || this . blockType == Verifications ) ) {
140+ rewriteVerify ( invocation , null , "" );
140141 return ;
141142 }
142-
143- boolean hasTimes = false ;
144143 if (mockInvocationResults .getTimes () != null ) {
145- hasTimes = true ;
146144 rewriteVerify (invocation , mockInvocationResults .getTimes (), "times" );
147145 }
148146 if (mockInvocationResults .getMinTimes () != null ) {
149- hasTimes = true ;
150147 rewriteVerify (invocation , mockInvocationResults .getMinTimes (), "atLeast" );
151148 }
152149 if (mockInvocationResults .getMaxTimes () != null ) {
153- hasTimes = true ;
154150 rewriteVerify (invocation , mockInvocationResults .getMaxTimes (), "atMost" );
155151 }
156- if (!hasResults && !hasTimes ) {
157- rewriteVerify (invocation , null , "" );
158- }
159152 }
160153
161154 private void removeBlock () {
162155 methodBody = JavaTemplate .builder ("" )
163156 .javaParser (JavaParser .fromJavaVersion ())
164157 .build ()
165158 .apply (new Cursor (visitor .getCursor (), methodBody ), nextStatementCoordinates );
166- if (bodyStatementIndex == 0 ) {
167- nextStatementCoordinates = methodBody .getCoordinates ().firstStatement ();
168- } else {
169- setNextStatementCoordinates (0 );
170- }
159+ setNextStatementCoordinates (0 );
171160 }
172161
173- private void rewriteResult (J .MethodInvocation invocation , List <Expression > results ) {
174- String template = getWhenTemplate (results );
162+ private void rewriteResult (J .MethodInvocation invocation , List <Expression > results , boolean hasTimes ) {
163+ boolean lenient = this .blockType == NonStrictExpectations && !hasTimes ;
164+ String template = getWhenTemplate (results , lenient );
175165 if (template == null ) {
176166 // invalid template, cannot rewrite
177- rewriteFailed = true ;
167+ this . rewriteFailed = true ;
178168 return ;
179169 }
180170
181171 List <Object > templateParams = new ArrayList <>();
182172 templateParams .add (invocation );
183173 templateParams .addAll (results );
184174 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 );
175+ if (this .rewriteFailed ) {
176+ return ;
177+ }
178+
179+ setNextStatementCoordinates (++numStatementsAdded );
180+ // do this last making sure rewrite worked and specify onlyifReferenced=false because framework cannot find static
181+ // reference for when method invocation when another static mockit reference is added
182+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "when" , false );
183+ if (lenient ) {
184+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "lenient" );
196185 }
197186 }
198187
@@ -218,29 +207,37 @@ private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression t
218207 verifyCoordinates = methodBody .getCoordinates ().lastStatement ();
219208 }
220209 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- }
210+ if (this .rewriteFailed ) {
211+ return ;
212+ }
225213
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- }
214+ if (this .blockType == Verifications ) {
215+ setNextStatementCoordinates (++numStatementsAdded ); // for Expectations, verify statements added to end of method
216+ }
217+
218+ // do this last making sure rewrite worked and specify onlyifReferenced=false because framework cannot find the
219+ // static reference to verify when another static mockit reference is added
220+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , "verify" , false );
221+ if (!verificationMode .isEmpty ()) {
222+ visitor .maybeAddImport (MOCKITO_IMPORT_FQN_PREFX , verificationMode );
232223 }
233224 }
234225
235226 private void setNextStatementCoordinates (int numStatementsAdded ) {
227+ if (numStatementsAdded <= 0 && bodyStatementIndex == 0 ) {
228+ nextStatementCoordinates = methodBody .getCoordinates ().firstStatement ();
229+ return ;
230+ }
231+
236232 // the next statement coordinates are directly after the most recently written statement, calculated by
237233 // subtracting the removed jmockit block
238- int nextStatementIdx = bodyStatementIndex + numStatementsAdded - 1 ;
239- if (nextStatementIdx >= this .methodBody .getStatements ().size ()) {
240- rewriteFailed = true ;
241- } else {
242- this .nextStatementCoordinates = this .methodBody .getStatements ().get (nextStatementIdx ).getCoordinates ().after ();
234+ int lastStatementIdx = bodyStatementIndex + numStatementsAdded - 1 ;
235+ if (lastStatementIdx >= this .methodBody .getStatements ().size ()) {
236+ this .rewriteFailed = true ;
237+ return ;
243238 }
239+
240+ this .nextStatementCoordinates = this .methodBody .getStatements ().get (lastStatementIdx ).getCoordinates ().after ();
244241 }
245242
246243 private boolean rewriteTemplate (String template , List <Object > templateParams , JavaCoordinates
@@ -258,10 +255,10 @@ private boolean rewriteTemplate(String template, List<Object> templateParams, Ja
258255 return methodBody .getStatements ().size () > numStatementsBefore ;
259256 }
260257
261- private @ Nullable String getWhenTemplate (List <Expression > results ) {
258+ private @ Nullable String getWhenTemplate (List <Expression > results , boolean lenient ) {
262259 boolean buildingResults = false ;
263260 StringBuilder templateBuilder = new StringBuilder ();
264- if (this . blockType == NonStrictExpectations ) {
261+ if (lenient ) {
265262 templateBuilder .append (LENIENT_TEMPLATE_PREFIX );
266263 }
267264 templateBuilder .append (WHEN_TEMPLATE_PREFIX );
@@ -422,5 +419,9 @@ private static class MockInvocationResults {
422419 private void addResult (Expression result ) {
423420 results .add (result );
424421 }
422+
423+ private boolean hasAnyTimes () {
424+ return times != null || minTimes != null || maxTimes != null ;
425+ }
425426 }
426427}
0 commit comments