I had a problem with EasyMock ... I had to validate a call into a method that passed an exception. Exceptions don't compare well to each other so this was causing problems.
Fortunately, EasyMock has an escape clause for this purpose, you can override how it compares expected vs. actual arguments for a particular method. Thus:
public class ExceptionAwareArgumentsMatcher extends AbstractMatcher { protected boolean argumentMatches(Object expected, Object actual) { if (expected instanceof Throwable) return expected.getClass().equals(actual.getClass()); return super.argumentMatches(expected, actual); } }
This just checks that the exception passed into the mock object is the right type, not that it is any particular instance.
In use, it looks like this:
MockControl c = MockControl.createStrictControl(ErrorHandler.class); ErrorHandler eh = (ErrorHandler) c.getMock(); eh.error(LOG, "Unable to order cartoon character 'wilma' due to dependency cycle:" + " A cycle has been detected from the initial goal [wilma]", null, new CyclicGoalChainException(new Goal(""))); c.setMatcher(new ExceptionAwareArgumentsMatcher()); c.replay();
3 comments:
Aha... Now I can do the same for arrays, a problem I had been having for a little while now. Nice...
-Harish
Harish, Did you try it out for an array? Did it work fine for you? If yes can you please pass down the solution to me. Thanks!
Richa
Harish, Did you try it out for an array? Did it work fine for you? If yes can you please pass down the solution to me. Thanks!
Richa
Post a Comment