title: Mocking a class with multiple mocked dependency as constructor
body: <p>I've been trying to mock my service/business class to start testing my app but there are many "dependencies" on other class, mainly Repositories and a stub passed to those repository.
  I've been trying to set this up in order to be able to unit test my service but i keep on getting errors and i don't really know if my setup is right or not :</p>
  
  <p>I haven't included the Interfaces as i felt there weren't necessary but could if needed.</p>
  
  <pre><code>    public abstract class  DemoRepo&lt;T&gt; implements Repository&lt;T&gt; {
  
          private dbStub stub;
          private Class&lt;T&gt; clazz;
  
  
          public DemoRepo(DbStub stub,Class&lt;T&gt; clazz){
              this.clazz = clazz;
              this.stub = stub;
          }
  
          @Override
          public void add(T item) {
              stub.inSert(item);
          }
          @Override
          public void update(T item) {
              stub.update(item);
          }
          ...
  
      }
  </code></pre>
  
  <p>Then those 2 repository extends the abstract class for each class</p>
  
  <pre><code>    public class Class1Repo extends DemoRepo&lt;Class1&gt; {
          public Class1Repo(DbStub stub) {
              super(stub,Class1.class);
          }
      }
  
  
      public class Class2Repo extends DemoRepo&lt;Class2&gt; {
          public Class2Repo(DbStub stub) {
              super(stub,Class2.class);
          }
      }
  </code></pre>
  
  <p>Then same for the abstract service which use the 2 repositories created before.</p>
  
  <pre><code>    public abstract class AbstractService implements ClassService {
          private Repository&lt;Class1&gt; class1Repository;
          private Repository&lt;Class2&gt; class2Repository;
  
          public AbstractService(Repository&lt;Class1&gt; class1Repository, Repository&lt;Class2&gt; class2Repository) {
              this.class1Repository = class1Repository;
              this.class2Repository = class2Repository;
          }
  
          public boolean itemValiation(String itemId){
              Class1 item = class1Repository.findOne(itemId);
              item.setValidated(true);
              class1Repository.update(item);
              return true;
          }
  
          .....
  
      }
  </code></pre>
  
  <p>Finally this the service what i'm trying to test :</p>
  
  <pre><code>    public class DemoImplService extends AbstractService {
  
          public DemoImplService(Class1Repo c1repo,Class2Repo c2repo){
              super(c1repo, c2repo);
          }
      }
  </code></pre>
  
  <p>But as you can see i'm gonna need to pass 2 mocked repositories which themself are instantiated from a mocked " stub" and that's my issue.</p>
  
  <p>I've tried to do it many different ways but here is the most recent one giving me trouble :</p>
  
  <pre><code>class DemoImplServiceTest {
      @Mock
      private DbStub stub;
  
      private DemoImplService service;
  
      @InjectMocks
      private Class1Repo repo;
  
      @InjectMocks
      private Class2Repo repor;
  
  
      @Before
      public void setUp() {
          MockitoAnnotations.initMocks( this );
          Class1Repo repo = new Class1Repo(stub);
          Class2Repo repor = new Class2Repo(stub);
          DemoImplService service = new DemoImplService(repo,repor);
      }
  
      @Test
      void itemValiation() {
          Class1 c1 = new Class1();
          **when (repo.findOne("1")).thenReturn(c1);**
          //java.lang.NullPointerException
      }
  
  }
  </code></pre>
  
  <p>My logic here was to try to use the mock / injectmocks mechanism and then just mocking the repository as i don't really need to mock the Stub but it's not compiling i'm getting <code>java.lang.NullPointerException</code></p>
  
  <p>I've also tried setting up the 2 repository with       <code>ReflectionTestUtils.setField</code> but i'm not even compiling this way so i gave up.</p>
  
  <hr>
  
  <p>Update :</p>
  
  <p>new class</p>
  
  <pre><code>class DemoImplServiceTest {
  
  @Mock
  private Class1Repo repo;
  @Mock
  private Class2Repo repor;
  @InjectMocks
  private DemoImplService service;
  
  
  @Before
  public void setUp() {
      MockitoAnnotations.initMocks( this );
      Class1 object = new Class1();
      when (repo.findOne("1")).thenReturn(object);
  }
  
  @Test
  void itemValiation() {
     boolean updated = service.itemValiation("1");
     assertTrue( updated );
     verify( repo ).findOne("1");
     ArgumentCaptor&lt;Class1&gt; class1Captor = ArgumentCaptor.forClass( Class1.class );
     verify( repo ).update( class1Captor.capture() );
     Class1 updatedCclass1 = class1Captor.getValue();
     assertTrue( Class1.isValidated() );
  }
  </code></pre>
  
  <p>This line <code>boolean updated = service.itemValiation("1");</code> is what's failling : java.lang.NullPointerException</p>
  
  <p>I've tried declaring  </p>
  
  <pre><code>    Class1 object = new Class1();
      when (repo.findOne("1")).thenReturn(object);
  </code></pre>
  
  <p>inside the <code>void itemValiation()</code> func but that didn't change anything</p>
  
  <p>Last thing is that if i add the method <code>service = new DemoImplService(repo,repor);</code> inside the test function i'm getting a different null pointer exception this time located in the AbstractService inside the itemValidation method for that line <code>class1Repository.update(item);</code></p>
  
  <p>Update 2</p>
  
  <p>The full exception without instantiating the class :</p>
  
  <pre><code>    java.lang.NullPointerException
          at com.jd.mypackage.test.services.ImplServiceTest.itemValiation(ImplServiceTest.java:187)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:316)
          at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:114)
          at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.lambda$invokeTestMethod$6(MethodTestDescriptor.java:171)
          at org.junit.jupiter.engine.descriptor.MethodTestDescriptor$$Lambda$141/655381473.execute(Unknown Source)
          at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
          at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.invokeTestMethod(MethodTestDescriptor.java:168)
          at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.execute(MethodTestDescriptor.java:115)
          at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.execute(MethodTestDescriptor.java:57)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:81)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$109/981661423.execute(Unknown Source)
          at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:76)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:91)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$109/981661423.execute(Unknown Source)
          at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:76)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:91)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$109/981661423.execute(Unknown Source)
          at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:76)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
          at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
          at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
          at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:87)
          at org.junit.platform.launcher.Launcher.execute(Launcher.java:93)
          at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)
          at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
          at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
          at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>