пятница, 24 сентября 2010 г.

Wrote test, use mockito...drink mockito!

Hi all!

In our project we have parts that are written by our outsource partners. We are using their services in our code. Services injected from spring to our beans.

Yesterday, they made a weekly commit, and I've found out that my tests were broken :( One of their developer had commented my stub that implemented their interface. They said that he didn't want to change any code although I needed my stub to collect and analyze input in it.

I started solving this problem learning about mock objects. On my previous work, one of my coworkers used mockito and he liked it a lot :) I've found the information about it here: site. I suppose it's good that the last version was released in may. Also, I like projects that have good documentation and have *-all.jar. Mockito has all of that!

I needed only one method from two interfaces. And I've written simple mock objects and inject this mocks in spring.


SomeService mockService = Mockito.mock(SomeService.class);
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
String[] users = (String[])invocation.getArguments()[0];
String title = (String)invocation.getArguments()[1];
//Collect information here.
return null;
}
}).when(mockService).
someMethod(Mockito.<string[]>any(), Mockito.anyString());

Also, I have got some troubles when injecting this service in spring: I could not do something like this:
myService.setSomeService(mockService);

because I am using @Transactional and having Proxy object. After some google searches I've found solution for this(I think, that it's not ideal, because I found some info about @Configurable beans and wrote TODO ;))

((MyServiceImpl)( ((Advised)myService).getTargetSource().getTarget())).setSomeService(mockService);


I commited this and...drink mockito :)!

Комментариев нет:

Most popular

Authors