|
Re: Sakai Test-Harness: Unit Test Providers: msg#00735cms.sakai.devel
Thomas Amsler wrote: Hello, 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.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> |
|---|---|---|
| Previous by Date: | Re: Java 5 woes: 00735, Joseph Dane |
|---|---|
| Next by Date: | Re: Correct way to check if user is logged in?: 00735, Glenn R. Golden |
| Previous by Thread: | Sakai Test-Harness: Unit Test Providersi: 00735, Thomas Amsler |
| Next by Thread: | Sakai 2.1.1 Grouping External Students: 00735, Eric Lo |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |