title: How to use ArgumentCaptor in mockito
body: <p>I'm learning Mockito and Unit Testing in general. I want to learn how to unit test better by using Argument Captor. I'm using jdbc to handle my SQL statement. I have a method that inserts a user into my DB.</p>
  
  <pre><code>public void insert(User user) {
     String sql = "INSERT INTO user (id) VALUES ?";
     jdbcTemplate.update(new PreparedStatementCreator() {
       @Override
       public PreparedStatement createPreparedStatement(Connection connection) {
         final PreparedStatement ps = connection.prepareStatement(sql);
         ps.setString(1, user.getId().trim());
         return ps;
       }
     });
   }
  </code></pre>
  
  <p>Below is the test that I'm trying to write with ArgumentCaptor.</p>
  
  <pre><code>    @Test
        public void testInsert() {
          User user = new User("testID");
          ArgumentCaptor&lt;PreparedStatementCreator&gt; captor = ArgumentCaptor.forClass(PreparedStatementCreator.class);
          insert(user);
          verify(mockJdbc, times(1)).update(captor.capture());
          PreparedStatementCreator actual = captor.getValue();
          assertEquals(??, actual.createPreparedStatement(??));
        }
  </code></pre>
  
  <p>Any advice or insight on what should be in the '??' for the assert statement or if this is the correct way to use Argument Captor?</p>
  
  <p>Thank You</p>
  
  <p>Edit:</p>
  
  <pre><code>      @Test
        public void testInsert() throws SQLException {
              ArgumentCaptor&lt;PreparedStatementCreator&gt; captor = ArgumentCaptor.forClass(PreparedStatementCreator.class);
              PreparedStatement ps = mockConnection.prepareStatement("INSERT INTO user (id) VALUES ?";);
              ps.setString(1, user.getId().trim());
              insert(user);
              verify(mockJdbcTemplate, times(1)).update(captor.capture());
              PreparedStatementCreator actual = captor.getValue();
              assertEquals(ps, actual.createPreparedStatement(mockConnection));
            }
  </code></pre>