Java – simple JDBC connection testing using JUnit

I want to simply test the JDBC connection. I don't use the framework, only JDBC and JUnit Can I perform this test with JUnit? I don't know how to test the loading driver. Please give me some examples of connection testing

Connect client:

package newpackage.db;

import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;

public class sqlConnection {
    private Connection con;
    private String name;
    private String url;
    private String password;
    private String driver;

    public sqlConnection() {
        this.name = "root";
        this.password = "12345";
        this.url = "";
        this.driver = "com.MysqL.jdbc.Driver";
    }

    public Connection getConnection() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(sqlConnection.class.getName()).log(Level.SEVERE,null,ex);
        }

        return null;
    }
}

Test case:

public void testDriverManager() {
    sqlConnection conClient = new sqlConnection();
    assertEquals(conClient.getConnection(),...);
    or
    assertTrue(conClient.getConnection(),...);
}

Solution

Basically, you can follow this guide to establish a connection for the driver manager

http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

The best test of connection is to read some basic content from the database For example Select 1 from double

Please note that this test needs to run the database! Keep the number of these comprehensive tests small because they do not scale well and need to be given a specific environment It enables, for example, continuous integration Hudson is a little difficult because you have to install the database on all continuous integration server nodes and keep it running

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>