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
>