title: How to mock jdbc connection and resultSet using Mockito in TestNG
body: <p>I have to write some unit tests but I have problem with mocking <code>ResultSet</code> and jdbc <code>Connection</code>. </p>
  
  <p>I have this method:</p>
  
  <pre><code>@Test
  public void test3() throws SQLException, IOException {
  
      Connection jdbcConnection = Mockito.mock(Connection.class);
      ResultSet resultSet = Mockito.mock(ResultSet.class);
  
      Mockito.when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
      Mockito.when(resultSet.getString(1)).thenReturn("table_r3").thenReturn("table_r1").thenReturn("table_r2");
      Mockito.when(jdbcConnection
              .createStatement()
              .executeQuery("SELECT name FROM tables"))
              .thenReturn(resultSet);
  
      //when
      List&lt;String&gt; nameOfTablesList = null;
      try {
          nameOfTablesList = Helper.getTablesName(jdbcConnection);
      } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      //then
      Assert.assertEquals(nameOfTablesList.size(), 3);
  }
  </code></pre>
  
  <p>And error is showing in line <code>executeQuery("SELECT name FROM tables")</code> and it sounds like this: </p>
  
  <pre><code>java.lang.NullPointerException HelperTest.test3(HelperTest.java:71)
  </code></pre>
  
  <p>Any ideas whats going wrong?</p>