Tuesday 13 December 2011

Vargs bites agian

This caught me out the other day. Perhaps it shouldn't have done, but I thought it was interesting enough to write down.

The method looked like
public void myMethod(String... something);

I was trying to mock it out and was expecting it to be called with an empty argument list...

I initially tried to clever and though, "well, I know that arguments are autoboxed into an array, so I guess this will happen"
     EasyMock.expect(myObject.myMethod(EasyMock.arrEq(String[]{})));


hmmmm... no joy.... it went pop at runtime...

turns out that vargs is even more surprising that I first thought....

the expectation I was looking for was
     EasyMock.expect(myObject.myMethod());

simple when you've seen it!

No comments:

Post a Comment