Mockito for jndi lookups?

3,774 views
Skip to first unread message

tamu

unread,
Jun 26, 2008, 2:45:58 AM6/26/08
to mockito
Hi,

I am an EasyMock user & have just recently tried Mockito & luving it
especially it's simplicity. I though want to ask whether there's any
way to mock jndi lookups. I need to unit test something like this:

WorkspaceService locator = (WorkspaceService)
initialContext.lookup("java:comp/env/service/WorkspaceService");

Regards,
Tamu

szczepiq

unread,
Jun 26, 2008, 5:26:02 AM6/26/08
to moc...@googlegroups.com
Hi,

I haven't been using jndi for years now so I don't remember any
related testing patterns.

Looking at your example: lookup() is ordinary query method that can be stubbed:

stub(initialContext.lookup("java:comp/env/service/WorkspaceService"))
.toReturn(workspaceService);

Cheers,
Szczepan Faber

Sagun Gurung

unread,
Jun 26, 2008, 10:30:40 PM6/26/08
to moc...@googlegroups.com
Thanks for the response. The thing that you suggested was exactly the first thing I tried by injecting the initial context for the test method & stubbing the response. However, it looks for the container which is good. Eventhough I would want to try out-of container for the unit testing, I did try again by starting the application server (Websphere 6.1) just for the sake of curiousity but still couldn't find the name.

Anyway, I would want to do jndi lookup test out of the container and I am looking at MockEJB & think it could be the answer but I'm not sure at the moment. Would want to give it a try.

Thanks,

John

unread,
Jun 26, 2008, 11:23:57 PM6/26/08
to mockito
Have you tried refactoring your code to separate your business login
from your infrastructure related wiring. For example:

public interface WorkspaceServiceFactory {
WorkspaceService lookupWorkspaceService();
}

public class DefaultWorkspaceServiceFactory implements
WorkspaceService {
public WorkspaceService lookupWorkspaceService() {
InitialContext initialContext = new InitialContext();

return WorkspaceService) initialContext.lookup("java:comp/env/
service/WorkspaceService");
}
}

public class WorkspaceServiceClient {
private WorkspaceServiceFactory factory;

public void execute() {
WorkspaceService service =
getFactory().lookupWorkspaceService();

//do something
}

public void setFactory(WorkspaceServiceFactory factory) {
this.factory = factory;
}

public WorkspaceServiceFactory getFactory() {
if (factory == null) {
factory = new DefaultWorkspaceServiceFactory();
}

return factory;
}

}

You could then mock the factory and inject the mock into your client.

You could also do the reverse. Refactor your business logic into a
class and unit test the class. The workspace service could be located
and injected to this class.

Good Luck,
John

On Jun 26, 7:30 pm, "Sagun Gurung" <sagungur...@gmail.com> wrote:
> Thanks for the response. The thing that you suggested was exactly the first
> thing I tried by injecting the initial context for the test method &
> stubbing the response. However, it looks for the container which is good.
> Eventhough I would want to try out-of container for the unit testing, I did
> try again by starting the application server (Websphere 6.1) just for the
> sake of curiousity but still couldn't find the name.
>
> Anyway, I would want to do jndi lookup test out of the container and I am
> looking at MockEJB & think it could be the answer but I'm not sure at the
> moment. Would want to give it a try.
>
> Thanks,
>
> On Thu, Jun 26, 2008 at 7:26 PM, szczepiq <szcze...@gmail.com> wrote:
>
> > Hi,
>
> > I haven't been using jndi for years now so I don't remember any
> > related testing patterns.
>
> > Looking at your example: lookup() is ordinary query method that can be
> > stubbed:
>
> > stub(initialContext.lookup("java:comp/env/service/WorkspaceService"))
> >    .toReturn(workspaceService);
>
> > Cheers,
> > Szczepan Faber
>

Sagun Gurung

unread,
Jun 27, 2008, 2:36:33 AM6/27/08
to moc...@googlegroups.com
Hi John,

My method looks something like this:

*********************************************
    protected synchronized WorkspaceService getWorkspaceService() throws DataAccessException {
        log.trace("Entering getWorkspaceService");
        if (wsWorkspace == null) {
            // workspaceService has not been initialised; search for it in JNDI repository
            try {
                ctx = new InitialContext();
                WorkspaceServiceService locator = (WorkspaceServiceService) ctx.lookup("java:comp/env/service/WorkspaceServiceService");
                wsWorkspace = locator.getWorkspaceServicePort();

            } catch (NamingException nex) {
                log.error("Cannot fetch WorkspaceService: Naming exception: " + nex.getMessage(), nex);
                throw new DataAccessException("NamingException: " + nex.getMessage(), nex);

            } catch (ServiceException svx) {
                log.error("Cannot fetch WorkspaceService: Service exception: " + svx.getMessage(), svx);
                throw new DataAccessException("ServiceException: " + svx.getMessage(), svx);
            }
        }
        log.trace("Leaving getWorkspaceService");
        return wsWorkspace;
    }
*********************************************
The only purpose of this method is to pass me the service after a correct jndi lookup. I have used mockito to test the throwing exception path but for I need to unit test the normal path for jndi lookup. The thing that i had tried was injecting the context which led to the issues that I got in the previous post.

Regards,

John

unread,
Jun 27, 2008, 10:52:35 AM6/27/08
to mockito
OK.

I assume the problem that you had mocking the InitialContext is that
the constructor actually is executing some code that fails. How about
modifiying your code to rely on javax.naming.Context. You can safely
mock the Context object and inject a real InitialContext when you are
running in a real application environment.

John
Reply all
Reply to author
Forward
0 new messages