无法理解警告消息:预期:<interface java.sql.connection =“”>但是:<class com.mysql.jdbc.jdbc4connection =“”> </class> </interface>

时间:2012-05-13 12:36:53

标签: java jdbc

我在单元测试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;
    }
}

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:4)

你的断言从根本上说是错误的。

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