title: how to apply toAnswer method (mockito testing)?
body: <p>I have a method that looks like this:</p>
  <pre><code>public  class MainClass{
  @Autowired
  OtherClass otherClass;
  
  public ObjectType1 method1(){
     ObjectType1 object = new ObjectType1();
     object.setField1(&quot;field1Value&quot;);
     otherClass.method5(object) // method 5 is huge, modifies  the field2 of &quot;object&quot;
     return object; //an object with field1 and field2 modified
  }
  }
  </code></pre>
  <p>what is the best way to test this in mockito? in code/pseudocode mixture, I am looking for something like this in my test class</p>
  <pre><code>@Test
  public void testMethod1() {
    MainClass class1 = new MainClass();
  
    ObjectType1 newObject = class1.method1();
    //in method1, a new ObjectType1 is created, is there a way to mock the calling of method5?
  
    assertNotNull(newObject.field1);
  }
  </code></pre>
  <p>From the research it seems doAnswer can do the trick, however I have not really understood any of the examples I've seen online</p>