Search code examples
javamockingtddguiceroboguice

Injecting dependency with Guice in non-constructor method


I'm working on a component that creates HTTP requests dynamically, and I'd like to be able to mock those requests for unit testing.

Currently the implementation looks something like this:

class ModelClass {
     public void populate() {
          HTTPRequest request = new HTTPRequest();
          //configure request...
          request.send();
     }
}

Is there a way to use Guice to instantiate request so I can replace it with an instance of a mock class for testing? The nearest I can figure out would be to add an injector as instance variable of ModelClass:

class ModelClass {
     private final Injector injector;

     ModelClass(Injector injector){
          this.injector = injector;
     }

     public void populate() {
          HTTPRequest request = injector.getInstance(HTTPRequest.class);
          //configure request...
          request.send();
     }
}

But that's basically like using a factory, which misses the point of Guice entirely.


Solution

  • You can inject a provider which provides 'HTTPRequest' instances in your code.

    class ModelClass {
    
       @Inject
       Provider<HTTPRequest> httpRequestProvider;
    
       public void populate() {
          HTTPRequest request = httpRequestProvider.get();
       }
    
    }
    

    Then, in your test code, you can mock the 'httpRequestProvider' to return mock 'HTTPRequest' instances.

    Provider<HTTPRequest> mockHttpRequestProvider = mock(Provider.class);
    when(mockHttpReqestProvider.get()).thenReturn(yourMockHTTPRequestObject);
    // Set this mock provider to the ModelClass instance. (You may have to use reflection)
    

    Info on injecting providers: https://github.com/google/guice/wiki/InjectingProviders