title: How to JUnit test that my mocked method is called with the parameter i want (not just id, but deep equal matcher)?
body: <p>Here is a part of my method that i want to test:</p>
  
  <pre><code>protected void myMethod(MyObject o) {
       o.setComment("comment");
       MyObjectDAO.updateMyObject(o);
  }
  </code></pre>
  
  <p>Here is a part of my JUnit:</p>
  
  <pre><code>    @Mock
      private MyObjectDAO myObjectDaoMock;
  
      @Test
      public void testMyMethod() {
           MyObject o = new MyObject();
  
           // Run Test
           x.myMethod(o);
  
           // Control
           assertEquals("comment", o.getComment());
           verify(myObjectDaoMock).updateMyObject(o);
      }
  </code></pre>
  
  <p>The test is green; we are now testing whether the comment attribute of myObject is set, and the update method is called with myObject. But <strong>not</strong> whether the update method is called <strong>with the updated attribute</strong> (after setComment method). When we change the position of two lines in myMethod (first updating the object, then setting its attribute), our test is still green.</p>
  
  <p>I thought that the ArgumentCaptor can be useful here. So i made this one:</p>
  
  <pre><code>    @Mock
      private MyObjectDAO myObjectDaoMock;
  
      @Test
      public void testMyMethod() {
           MyObject o = new MyObject();
  
           // Run Test
           x.myMethod(o);
  
           // Control
           ArgumentCaptor&lt;MyObject&gt; argumentCaptor = ArgumentCaptor.forClass(MyObject.class);
           verify(myObjectDaoMock).updateMyObject(argumentCaptor.capture());
           MyObject oActual = argumentCaptor.getValue();
           assertEquals("comment", oActual.getComment());
      }
  </code></pre>
  
  <p>... hoping that the ArgumentCaptor will capture that state of the object, by which the update method is called, so i can be sure that the update method is called with the updated comment attribute. The test is green again. <strong>But it still does not test cleverly.</strong> When we change the position of two lines in myMethod again (first updating the object, then setting its attribute), our test is still green. </p>
  
  <p>I understand that, the ArgumentCaptor does not create another attribute for himself (argumentCaptor.getValue()), it is the reference of the original object. So since java works with <strong>Reference by Value</strong>, for JUnit it does not make any difference whether i update the object before or after, as long as the objectIds are same.</p>
  
  <p><strong>How can i actually test that the updateObject method is called with the updated value of myObject?</strong></p>
  
  <p><strong><em>EDIT:</strong> Another idea was (thanks to @Crazyjavahacking) to mock also myObject (although i don't want that), and define a call-order with 2 mocks:</em></p>
  
  <pre><code>InOrder inOrder = inOrder(o, myObjectDAOMock);
  
  inOrder.verify(o).setComment("comment");
  inOrder.verify(myObjectDAOMock).updateMyObject(o);
  </code></pre>
  
  <p>That still does not test that the method is called with the updated value. It just test the order. When we change our code: </p>
  
  <pre><code>o.setComment("comment"); 
  o.setComment("comment2"); 
  MyObjectDAO.updateMyObject(o); 
  </code></pre>
  
  <p>.. our test would still run green, since the order is correct based on our order definition. But the method is not called with the updated value "comment" as we want.</p>