简体   繁体   中英

How to mock a database connection with JUnit?

Simply can't find a concrete example where a db connection is mocked. Preferred with Mockito.

public Connection getCon() throws SQLException, Exception {
        Properties login = new Properties();

        TimeZone timeZone = TimeZone.getTimeZone("GMT+2:00");
        TimeZone.setDefault(timeZone);

        Connection conn = null;
        conn = DriverManager.getConnection(url, username, password);

        return conn;
}

Want to test this method...

All you need is to mock static class DriverManager. Here is an answer to exactly your issue: Mocking static methods with Mockito

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM