logo       

Re: Sakai Test-Harness: Unit Test Providers: msg#00735

cms.sakai.devel

Subject: Re: Sakai Test-Harness: Unit Test Providers

Thomas Amsler wrote:
Hello,

I wanted to check if anybody has used the Test-Harness to Unit test their custom provider implementations.

I haven't used it on a provider.

We created an ucd-enterprise-data-service that provides a DAO layer for our SIS. This service is accessed by our custom providers. We are successfully unit testing this service with the help of the Test-Harness. Now we would like to unit test our provider implementation. To do so, this would require to create a bunch of mock objects such as UserEditMock etc. that we cold pass to the provider methods. For now we just test APIs that use primitive types such as "userExits(String userId)" but we would like to test "getUser(UserEdit userEdit)", etc. methods as well.

Has anybody done this, and if so, are there such mock objects somewhere?
You can make use of Java's dynamic proxies to mock up your own objects. I added a little support for this (just a convenience method, really) in the test harness. Here's a sample of how to use dynamic proxies for mocking up collaborators...

There are probably more slick ways to do this with third party libraries, but I wanted to keep it simple.

Hope it helps,
Josh


package org.sakaiproject.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.sakaiproject.service.legacy.user.User;
import org.sakaiproject.service.legacy.user.UserDirectoryService;

public class ProxyUnitTest {
public static void main(String[] args) {
UserDirectoryService uds = (UserDirectoryService)SakaiTestBase.getServiceProxy(UserDirectoryService.class, new UdsHandler());
System.out.println(uds.getCurrentUser().getEmail());
}

static class UdsHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName());
if(method.getName().equals("getCurrentUser")) {
return SakaiTestBase.getServiceProxy(User.class, new UserHandler());
}
// We don't care about any other methods, so just return null
return null;
}
}

static class UserHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("getEmail")) {
return "foo@xxxxxxx";
}
// We don't care about any other methods, so just return null
return null;
}
}
}



--
Josh Holtzman
Educational Technology Services, UC Berkeley
jholtzman@xxxxxxxxxxxx
510.643.7380

----------------------
This automatic notification message was sent by Sakai Collab
(http://collab.sakaiproject.org/portal) from the DG: Development site.
You can modify how you receive notifications at My Workspace > Preferences.




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise