Hi Guice Gurus,

I am gradually introducing DI withGuice 2.x in my application. I am
hit upon a problem for which I hope you might be able to help me.

public class Service {

  private final String serviceType;

 @Inject
 public Service(String serviceType) {
    this.serviceType = serviceType;
 }

 @Inject
private Dao myDao;

// Setter and getter for Dao.

}

The service is called directly from the delegate which accesses the
injector configured using Guice's ServletContextListener. One of the
Module (ServiceModule) explicitly binds the string as follows
public class ClientTypeModule {
@Override
        protected void configure() {
                bind(String.class).toInstance("CRV_CLIENT_TYPE");
       }
}

My issue is that there can be other potential values for the
serviceType. To get around that, I thought I would use child injectors
in my code that invokes this instance.

public class Delegate {
@Inject
private Service service

// Settter for Service.

private final Injector injector;

@Inject
public class Delegate(Injector injector) {
  this.injector = injector;
}


public void callService()  throws DelegateException {
  Injector childInjector = injector.createChildInjector(new
ClientTypeModule()).injectMembers(this);
  try {
  service.doFoo();
} catch (DaoException de) {
  throw new DelegateException();
}

}
}

I find that  I am unable to mock Service instance to throw
DaoException to test exception handling as my mock is overridden by
child injector. Is there a work around for this that any one can
suggest? Any suggestion would be appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to