пятница, 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 :)!

среда, 1 сентября 2010 г.

Authentication problems in multithreading

In my application, I have a tree widget that using ajax for loading nodes. Tomcat create some threads to service requests. Each requests validate security ticket that stores in http session:

authenticationService.validate(ticket, null);

And some threads can not validate this ticket and service throws AuthenticationException. If you have particular problem, then you should look at alfresco's jira: ALF-3789. This bug was fixed in revision 21065 at trunk. To fix it in my code, I simple override bean:

<!--Override alfresco-->
    <bean name="ticketsCache" class="org.alfresco.repo.cache.EhCacheAdapter">
       <property name="cache">
          <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean" >
             <property name="cacheManager">
                <ref bean="internalEHCacheManager" />
             </property>
             <property name="cacheName">
                <value>org.alfresco.cache.ticketsCache</value>
             </property>
          </bean>
       </property>
    </bean>

After this all works fine :)

Most popular

Authors