-2

我在单元测试中收到此消息expected:<interface java.sql.Connection> but was:<class com.mysql.jdbc.JDBC4Connection>

从我的代码:

@Test
public void connectionTest() throws SQLException{
    Connection conn = ConnectionManager.createConnection();
    assertEquals(Connection.class, conn.getClass());
    conn.close();
}

我用于获取连接的模拟类:

public class ConnectionManager {


    @SuppressWarnings("unused")
    public static Connection createConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/library";
        String name = "root";
        String password = "root";
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection =  DriverManager.getConnection(url, name, password);
        } catch (ClassNotFoundException e) {
            if (connection != null){
                connection.close();
            }
            throw new SQLException(e);
        }
        return connection;
    }
}

我的代码有什么问题?

4

1 回答 1

4

您的断言从根本上是错误的。

Connection.class是一个接口。
conn.getClass()将返回一个实现该接口的具体类。

于 2012-05-13T12:37:59.643 回答