Menu

#2 Pessimistic Initialisation

open
nobody
None
5
2002-05-09
2002-05-09
Anonymous
No

I've found it useful to initialise my mock objects to be very tight in
their expectations, so I can be sure that calls to verify() actually
do the right thing. The generic code to do this is as
follows:

public void clearExpectations() {
Field[] fields
= this.getClass().getDeclaredFields();
try {
for (int i = 0; i
< fields.length; i++) {
Field field = fields[i];
if
(field.getType() == ExpectationCounter.class) {

ExpectationCounter counter = ((ExpectationCounter)
field.get(this));
counter.setFailOnVerify();

counter.setExpectNothing();
}
if (field.getType() ==
ExpectationList.class) {
ExpectationList list =
((ExpectationList) field.get(this));

list.setFailOnVerify();
list.setExpectNothing();
}
}

}
catch (IllegalArgumentException e) {
}
catch
(IllegalAccessException e) {
}
}

Regards,

-Darren

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.