title: how to test method with resultSet and preparedStatement
body: <p>i would like to unit test my method, which return from database id of last inserted object:</p>
  <pre class="lang-java prettyprint-override"><code>     public int getLastId() throws SQLException {
          String query = &quot;select LAST_INSERT_ID();&quot;;
          PreparedStatement stmt = connection.prepareStatement(query);
          int id = -1;
          ResultSet resultSet = stmt.executeQuery();
          while (resultSet.next()) {
              id = resultSet.getInt(&quot;LAST_INSERT_ID()&quot;);
          }
          return id;
      }
  </code></pre>
  <p>and then i want to do same unit test like this:</p>
  <pre class="lang-java prettyprint-override"><code>    dataBaseService = mock(DataBaseService.class);
          resultSet = mock(ResultSet.class);
  
       @Test
      public void getLastIdIsCorrect() throws SQLException {
          //given
          int expectedId = 1;
          int expectedIncorrectId = 101;
          //when
          when(resultSet.getInt(&quot;LAST_INSERT_ID()&quot;)).thenReturn(1);
          when(dataBaseService.getLastId()).thenCallRealMethod();
          int result = dataBaseService.getLastId();
          //then
          Assert.assertEquals(expectedId, result);
          Assert.assertNotEquals(expectedIncorrectId, result);
      }
  </code></pre>
  <p>But i'm getting Null pointer exception at line with PreparedStatement stmt = connection.prepareStatement(query);</p>
  <p>Can you explain me please, how to do unit tests like this correctly? I have few methods left with the same problem and i'm still learning unit tests....</p>