java - EasyMock: How to Verify Method Order for Set of Values Where Order of Set Does Not Matter -


i have test in have set of specific values 2 different methods execute once each value in set. need check 2 methods called in specific order in relation each other, not in relation order of set of values. example:

string[] values = { "a", "b", "c" };  (...<loop on values...) {     methodone(value);     methodtwo(value); } 

it not matter order values in, need verify methodone() , methodtwo() called each value in set , methodone() called before methodtwo().

i know can create control , expect methodone() , methodtwo() each value, control.verify(), depends on values being in specific order.

is there elegant way this?

thanks

you can using andanswer().

basically, inside andanswer() methodone() set variable hold passed in value was.

then in andanswer() methodtwo() assert same argument matches saved methodone answer.

since each call methodone modify variable make sure methodtwo() called after methodone().

note solution not thread safe

first need hold variable methodone call. can simple class single field or array of 1 element. need wrapper object because need reference in ianswer requires final or final field.

private class currentvalue{     private string methodonearg;  } 

now expectations. here called class testing (the system under test) sut:

    string[] values = new string[]{"a", "b", "c"};      final currentvalue currentvalue = new currentvalue();      sut.methodone(isa(string.class));      expectlastcall().andanswer(new ianswer<void>() {          @override         public void answer() throws throwable {             //save parameter passed in our holder object             currentvalue.methodonearg =(string) easymock.getcurrentarguments()[0];             return null;         }      }).times(values.length); // once every element in values      sut.methodtwo(isa(string.class));     expectlastcall().andanswer(new ianswer<void>() {          @override         public void answer() throws throwable {             string value =(string) easymock.getcurrentarguments()[0];             //check make sure parameter matches              //the recent call methodone()              assertequals(currentvalue.methodonearg, value);             return null;         }      }).times(values.length); // once every element in values      replay(sut);     ... //do test     verify(sut); 

edit

you correct if using easymock 2.4 + can use new capture class argument value in cleaner way methodone(). however, may still need use andanswer() methodtwo() make sure correct values called in order.

here same code using capture

capture<string> capturearg = new capture<>();      sut.methodone(and(capture(capturearg), isa(string.class)));     expectlastcall().times(values.length);      sut.methodtwo(isa(string.class));     expectlastcall().andanswer(new ianswer<void>() {          @override         public void answer() throws throwable {             string value =(string) easymock.getcurrentarguments()[0];             assertequals(capturearg.getvalue(), value);             return null;         }      }).times(values.length);      replay(sut); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -