-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 に答える