2

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.

durron597
  • 31,968
  • 17
  • 99
  • 158
sak
  • 2,612
  • 24
  • 55
  • Consider using [Jukito](https://github.com/ArcBees/Jukito). It combines Guice, JUnit and Mockito into a single framework. – durron597 Apr 29 '15 at 14:18

1 Answers1

7

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

  • 2
    +1, but you can use [`Providers.of(yourMockHTTPRequestObject)`](http://google.github.io/guice/api-docs/3.0/javadoc/com/google/inject/util/Providers.html#of(T)) to skip the extra mock. – Jeff Bowman May 01 '15 at 17:09