title: Unit testing with mock that has mock inside
body: <p>I need to unit test a certain method that works on an object of type X. The X object has few normal fields like String, int etc. but it also has a List of types Y. This Y type is also quite complex. And to make my problem even more difficult, let's say that the type Y has another list of type Z.</p>
  
  <p>Ideally, I need to mock object X, but tell it to inject another mock of type Y inside, which should also inject another mock Z to itself (to Y). How should I do that?</p>
  
  <p>And if I have so many few-levels mocks how should I avoid writing 20 specifications of what these mocks should actually return upon calling their methods?</p>
  
  <p>Or is this a type of problem where I should use Builders and actually build a real object?</p>
  
  <p>Thanks alot,
  Bob.</p>
  
  <p>Edit:
  just an example usage code on top from my head:</p>
  
  <pre><code>public String produceImportantStringOfImportantData(ObjectX x) throws ParseException {
      StringBuilder textResult = new StringBuilder();
  
      List&lt;ObjectY&gt; listOfY = x.getListOfY();
  
      if (listOfY.isValid()) {
          for (ObjectY y : listOfY) {
              for (ObjectZ z : y.getListOfZ()) {
                  textResult.append("&lt;font color='green'&gt;").append(z.getField2).append("&lt;/font&gt;").append(": ").append(z.getSomething())
                          .append(" Label : ").append(z.getParentComponent.getField()))
                          .append(" some important labels: ").append(z.getField()))
                          .append(" some important labels ").append(y.getAnotherField))
                          .append(" different, important label: ").append(y.getField()))
                          .append("&lt;br/&gt;");
              }
          }
      } 
      return textResult.toString();
  }
  </code></pre>