Monthly Archives: September 2011

mockito matchers

mocking stuff with mockito is cool… take a function, setup expected input values and define a return result… easy when the input value is fix… and can not be bigger then…

ok what was my problem… i had a simple function, which i needed to mock: (a little more complex, with acutally complex objects and enums, but it would have in a problem with following aswell)

int methodName(String s, int i);

the problem was that the return int is similar then the i is greater then or equal a value… lets say 3 (which actually was the case in my case, but who cares…)… so the mocks:

Mockito.when(this.myMock.methodName(“Hello”, 1)).thenReturn(7);
Mockito.when(this.myMock.methodName(“Hello”, 2)).thenReturn(23);

Greater then is:

Mockito.when(this.myMock.methodName(Mockito.eq(“Hello”), AdditionalMathcer.geq(3))).thenReturn(63456);

The problem or the confusing thing is, when you add an Matcher, all parameters need to have a Matcher… :/

date and calendar

java.util.date was a really  logic thing for me… but now, all cool functions on Date are deprecated… crap… ok with Calendar it makes object-oriented-wise more sense… but how the hell do i get from an Date to the Calendar? Calendar offers only an getInstance() function, which actually sets the Calendar to current datetime… not really the thing i wanted…

Calender cal = Calender.getInstance();
cal.setTime(date);

is the solution… crap…