Vic
Vic

Reputation: 22041

Expect other parameters in EasyMock

Is there an easy way to do something like that with EasyMock?

Object a, b, c;
expect(a.getB("string1")).andReturn(a).anyTimes();
expect(a.getB("string2")).andReturn(b).anyTimes();
expect(a.getB(<ANYTHING_ELSE>)).andReturn(c).anyTimes();

Or should I implement my own implementation of IArgumentMatcher?

Upvotes: 0

Views: 142

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61148

You can use the andAnswer method of expect:

expect(a.getB((String)anyObject())).andAnswer(new IAnswer<MyClass>() {
    public MyClass answer() {        
        String in = (String) getCurrentArguments()[0];
        switch(in) {
            case: "string1":
                return a;
            case: "string2":
                return b;
            default:
                return c;
        }
    }
});

N.B: Switch on String requires Java 7.

Upvotes: 1

Related Questions