简体   繁体   English

如何使用 JUnit 模拟数据库连接?

[英]How to mock a database connection with JUnit?

Simply can't find a concrete example where a db connection is mocked.根本找不到模拟数据库连接的具体示例。 Preferred with Mockito.首选 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.您所需要的只是模拟静态类 DriverManager。 Here is an answer to exactly your issue: Mocking static methods with Mockito这是对您的问题的确切答案: 使用 Mockito 模拟静态方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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