java - Best practices for verifying recursive method calls with Mockito -


consider example class:

public class processor {   private final dependency dependency;    public processor(dependency dependency) {     this.dependency = dependency;   }    public void processusers(list<integer> userids, int attempt) {     if (attempt == 0) {       //log initial processing     } else if (attempt > 0 && attempt < 5) {       //log retry processing     } else {       //log processing limit exceeded       return;     }     list<integer> failedids = new arraylist<integer>();     (integer userid : userids) {       try {         processuser(userid);       } catch (exception ex) {         //logging         failedids.add(userid);       }     }     if (failedids.isempty()) {       //log ok     } else {       processusers(failedids, attempt + 1);     }   }    public void processuser(integer userid) throws exception{     //dependency can throw exception     dependency.call();   } } 

i verify method processusers calls when exception thrown. here testy test:

public class processortest {   @test   public void processshouldcallitselfwithfailedsublistwhenprocessingfails(){     dependency dependency = mock(dependency.class);     when(dependency.call()).thenthrow(exception.class);     processor processor = new processor(dependency);     processor.processusers(arrays.aslist(new integer[]{1,2,3}), 0);     //need verify processor call #processusers recursively     //because dependency thrown exception, how?   } } 

what best practices verifying method calls recursively under circumstances?

i'm using testng + mockito , verbose language called java

you can verify number of times dependency.call() invoked. tell retry worked - number of calls:

@test public void processshouldcallitselfwithfailedsublistwhenprocessingfails() {     dependency dependency = mock(dependency.class);     when(dependency.call()).thenreturn(1, 2).thenthrow(exception.class).         thenreturn(4);     processor processor = new processor(dependency);     processor.processusers(arrays.aslist(new integer[]{1, 2, 3}), 0);     verify(dependency, times(4)).call(); } 

this tell retry failed many exceptions:

@test public void processshouldfailaftertoomanyretries() {     dependency dependency = mock(dependency.class);     when(dependency.call()).thenthrow(exception.class);     processor processor = new processor(dependency);     final list<integer> userids = arrays.aslist(new integer[]{1, 2, 3});     final int expectedretries = 5;     processor.processusers(userids, 0);     verify(dependency, times(expectedretries * userids.size())).call(); } 

if call dependency used userid, check expected calls dependency.call(int userid) happened. tell of them went through given enough retries:

@test public void processshouldcallitselfwithfailedsublistwhenprocessingfails() {     dependency dependency = mock(dependency.class);     when(dependency.call(anyint())).         thenreturn(1, 2).thenthrow(exception.class).thenreturn(4);     processor processor = new processor(dependency);     processor.processusers(arrays.aslist(new integer[]{1, 2, 3}), 0);     verify(dependency).call(1);     verify(dependency).call(2);     verify(dependency, times(2)).call(3); }  @test public void processshouldfailaftertoomanyretries() {     dependency dependency = mock(dependency.class);     when(dependency.call(anyint())).thenthrow(exception.class);     processor processor = new processor(dependency);     final list<integer> userids = arrays.aslist(new integer[]{1, 2, 3});     final int expectedretries = 5;     processor.processusers(userids, 0);     (integer userid : userids) {         verify(dependency, times(expectedretries)).call(userid);     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -