title: How to mock DriverManager.getConnection?
body: <p>How do I mock the DriverManager.getConnection() method?</p>
  
  <p>I want to test my method setUpConnectiontoDB()</p>
  
  <p>I tried it with PowerMock, easyMock and Mokito itself. I didn't find anything usefull. </p>
  
  <p>My Code:</p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.util.Properties;
  
  public class MysqlDAO implements DAO {
      private final Properties properties = new Properties();
  
      public MysqlDAO(String configPath) {
          loadProperties(configPath);
      }
  
      private Properties loadProperties(String configPath) {
          try {
              properties.load(new FileInputStream(configPath));
          } catch (IOException e) {
              e.printStackTrace();
          }
          return this.properties;
      }
  
      @Override
      public Connection setUpConnectionToDB() {
          try {
              Class.forName("com.mysql.jdbc.Driver");
  
              return DriverManager.getConnection(
                      properties.getProperty("url"),
                      properties.getProperty("user"),
                      properties.getProperty("passwd"));
  
          } catch (ClassNotFoundException | SQLException e) {
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>