title: How to use mockito for testing Database connection
body: <p>I am using <code>Junit</code> to test my jersey api. I want to test DAO without a database. I tried using Mockito but still not able to use mock object to test the DAO which contains Hibernate calls to DB. I want to write <code>Junit</code> for my Helper class which calls the DAO.Can anyone provide a solution with some sample code to mock the DB Connections in DAO.</p>
  
  <p>EDIT : </p>
  
  <p><strong>Status.java</strong></p>
  
  <pre><code>@GET
  @Produces(MediaType.TEXT_PLAIN)
  public String getDBValue() throws SQLException {
      DatabaseConnectionDAO dbConnectiondao = new DatabaseConnectionDAO();
      String dbValue = dbConnectiondao.dbConnection();
      return dbValue;
  }
  </code></pre>
  
  <p><strong>DatabaseConnectionDAO.java</strong></p>
  
  <pre><code>private Connection con;
  private Statement stmt;
  private ResultSet rs;
  private String username;
  
  public String dbConnection() throws SQLException{
      try{
          Class.forName("com.mysql.jdbc.Driver");
          con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
          stmt = con.createStatement();
          rs =stmt.executeQuery("select * from test");
  
          while(rs.next()){
              username = rs.getString(1);             
          }           
      }catch(Exception e){
          e.printStackTrace();            
      }finally{
      con.close();    
      }
      return username;
  }
  </code></pre>
  
  <p><strong>TestDatabase.java</strong></p>
  
  <pre><code>@Test
  public void testMockDB() throws SQLException{
      DatabaseConnectionDAO mockdbDAO = mock(DatabaseConnectionDAO.class);
      Connection con = mock(Connection.class);
      Statement stmt = mock(Statement.class);
      ResultSet rs = mock(ResultSet.class);
  
      Client client = Client.create();
      WebResource webResource = client.resource("myurl");
      ClientResponse response = webResource.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
  
      verify(mockdbDAO).dbConnection();
  
      //when(rs.next()).thenReturn(true);
      when(rs.getString(1)).thenReturn(value);    
  
      actualResult = response.getEntity(String.class);
      assertEquals(expectedResult,actualResult );
  }
  </code></pre>