I am getting this message in unit test expected:<interface java.sql.Connection> but was:<class com.mysql.jdbc.JDBC4Connection>
from my code:
@Test
public void connectionTest() throws SQLException{
Connection conn = ConnectionManager.createConnection();
assertEquals(Connection.class, conn.getClass());
conn.close();
}
My mock class for getting connection:
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;
}
}
What is wrong with my code?
Your assert is fundamentally wrong.
Connection.classis an interface.conn.getClass()will return a concrete class that implements that interface.