Tag Archives: mockito

powermock

PowerMock is an extension for Mockito or EasyMock, which can mock really special things… static classes, Constructors, partial mocks,…

cool thing…. http://code.google.com/p/powermock/

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… :/

mockito

mockito is a pretty cool mocking framework. It is simple and logic… actually i am using it with interfaces, which makes it really logic and simple, dont know, who it works against normal classes… will test that soon 🙂

but first, how does it work with a simple calculator class:

HelloWorld helloWorld = Mockito.mock(HelloWorld.class);
Mockito.when(helloWorld.hello(Mockito.anyString())).thenReturn(“Hello”);

  • So first line initializes the mock.
  • Second line defines the return value of the function hello(String) when called with any string. So it allways returns Hello.