Monday, August 10, 2009

EasyMock IArgumentMatcher with Scala Howto

Its pretty trivial if you're a Scala savvy, but for the learners it might be a bit tricky. First thing, you should probably first get familiar with the java way of using IArgumentMatcher. To use it you should have a static method that calls EasyMock.reportMatcher with your implementation of IArgumentMatcher. To archive that you must create the method in an object. Its a workaround for not having static methods in Scala. Then implement the matcher, pattern matching is lots of help here, and remember that in Scala AnyRef is equivalent to Java's Object.

object TestMyClazz {
* for EZMock argument matcher
def eqMyClazz(obj: MyClazz) : MyClazz = {
EasyMock.reportMatcher(new MyClazzMatcher(obj))

class MyClazzMatcher(obj: MyClazz) extends IArgumentMatcher {
def matches(actual: AnyRef): Boolean = actual match {
case actual: MyClazz => //check things...
case null => //is null ok?
case _ => false
def appendTo(buffer: StringBuffer) : Unit = buffer append "some text..."
Done, now you just need to call eqMyClazz() with your expected value.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.