title: how to mock resultset and populate it using Mockito in Java
body: <p>I have code where I populate <code>Resultset</code> with <code>CallableStatement.executeQuery()</code>. I have mocked <code>ResultSet</code> and <code>CallableStatement</code> but in order to test the method i have to populate <code>ResultSet</code>. </p>
  
  <p>Here is the code from the method I am testing </p>
  
  <pre><code>ResultSet rset = cs.executeQuery();
  while (rset.next()) {
  IndexVolatilityImpl tsImpl = new IndexVolatilityImpl();
  tsImpl.setTradeDate(rset.getString("trade_date"));
  tsImpl.setTradeTime(rset.getString("trade_time"));
  tsImpl.setExprDate(rset.getString("expr_date"));
  tsImpl.setSymbol(rset.getString("symbol"));
  tsImpl.setTradePrice(rset.getDouble("trade_price"));
  tsImpl.setContractMonth(rset.getString("contract_month"));
  tsImpl.setMilliSecs(rset.getString("trade_time_thou"));
  colIndexVolatilityImpl.add(tsImpl);
  </code></pre>
  
  <p>I have mocked the CallableStatement and ResultSet now since they are mocked my rset comes up empty. I would like to populate Resultset and doing it as below</p>
  
  <pre><code>resultSetMock = Mockito.mock(ResultSet.class);
  Mockito.when(resultSetMock.getString("trade_date")).thenReturn("03/10/2011");
  Mockito.when(resultSetMock.getString("trade_time")).thenReturn("12:24:56");
  Mockito.when(resultSetMock.getString("expr_date")).thenReturn("03/19/2011");
  Mockito.when(resultSetMock.getString("symbol")).thenReturn("VIX1");
  Mockito.when(resultSetMock.getDouble("trade_price")).thenReturn(Double.valueOf("20.96"));
  Mockito.when(resultSetMock.getString("contract_month")).thenReturn("1");
  Mockito.when(resultSetMock.getString("trade_time_thou")).thenReturn("165");
  
  Mockito.doReturn(resultSetMock).when(callableStatementMock).executeQuery();
  </code></pre>
  
  <p>But <code>rset</code> is <code>null</code>.</p>