title: Mock and having real object for testing purposes using Guice
body: <p>How do I create an object in a Guice Test module that is used as a mock in one test, but requires to be real object in another.</p>
  
  <p>eg.
  Consider I have a class called ConfigService. This is injected into another class called UserService using constructor injection. During testing, I use a TestModule which has various classes and their mocks.</p>
  
  <p>TestModule.java:</p>
  
  <pre><code>public class TestModule extends AbstractModule{
      @Override
      public void configure() {
              ConfigService configService = Mockito.mock(ConfigService.class);
              bind(ConfigService.class).toInstance(configService);
  
              UserService userService = new UserService(configService);
  
              bind(UserService.class).toInstance(userService);
  
      }
  }
  </code></pre>
  
  <p>In UserServiceTest, I create an injector and use the instances from this TestModule.</p>
  
  <pre><code>Injector injector = Guice.createInjector(new TestModule());
  userService = injector.getInstance(UserService.class);
  configService = injector.getInstance(ConfigService.class);
  </code></pre>
  
  <p>This works fine, the place where I face a problem now is when I need to test ConfigService.class. </p>
  
  <p>If I want to use the same TestModule for the ConfigServiceTest, how do I now change the mock object of ConfigService I created earlier to an actual one for testing. The vice versa is also a problem -> ie. if I have a real object of ConfigService, how do I stub and mock the responses in UserService.class.</p>
  
  <p>Is there a way to achieve this or should I be creating separate test modules for mocks and real objects? Or am I going about the entire process in a wrong way?  </p>