用于连接数据库的JUNIT测试用例 [英] JUNIT test case for connection with database

查看:110
本文介绍了用于连接数据库的JUNIT测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我测试JDBC连接的代码

package com.sybase;


public class SybaseDBConnection {

    public static Properties prop = null;


    public static Connection getConnection(String databaseType) {

        Connection conn = null;
        // Properties prop = null;
        String database = null;
        String driver = null;
        String url = null;
        String user = null;
        String password = null;

        try {
            // prop = new Properties();
            // prop.load(SybaseDBConnection.class.getClassLoader()
            // .getResourceAsStream("com/properties/sybase.properties"));

            database = prop.getProperty("sybase." + databaseType
                    + ".databaseName");
            driver = prop.getProperty("sybase." + databaseType
                    + ".driverClassName");
            url = prop.getProperty("sybase." + databaseType + ".url");
            user = prop.getProperty("sybase." + databaseType + ".username");
            password = prop.getProperty("sybase." + databaseType + ".password");

            // String dbConUrl =
            // "jdbc:datadirect:sqlserver://nt64sl2003a.americas.progress.comsql2008;Port=1433;DatabaseName=test;User=test;Password=test;EnableBulkLoad=true;BulkLoadBatchSize="
            // + JDBC_BATCH_SIZE;

            String dbConUrl = url + SEMI_COLLAN + "DatabaseName=" + database
                    + SEMI_COLLAN + "User=" + user + SEMI_COLLAN + "Password="
                    + password + SEMI_COLLAN + "EnableBulkLoad=true"
                    + SEMI_COLLAN + "BulkLoadBatchSize=" + JDBC_BATCH_SIZE;

            System.out.println("The URL is : " + dbConUrl);

            SybDriver sybDriver = (SybDriver) Class.forName(driver)
                    .newInstance();
            sybDriver.setVersion(com.sybase.jdbcx.SybDriver.VERSION_6);
            DriverManager.registerDriver(sybDriver);
            conn = DriverManager.getConnection(dbConUrl);
        } catch (SQLException e) {
            System.err.println("Exception occured : SQLException : "
                    + e.getMessage());
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.err.println("Exception occured : InstantiationException : "
                    + e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.err.println("Exception occured : IllegalAccessException : "
                    + e.getMessage());
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.err.println("Exception occured : ClassNotFoundException : "
                    + e.getMessage());
            e.printStackTrace();
        }

        return conn;
    }

    public static void closeConnection(Connection conn) {
        try {
            if (null != conn) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public static void closeResultset(ResultSet rs) {
        try {
            if (null != rs) {
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public static void closePreparedStatement(PreparedStatement pstmt) {
        try {
            if (null != pstmt) {
                pstmt.close();
                pstmt = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public static void closeStatement(Statement stmt) {
        try {
            if (null != stmt) {
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public static String getProperty(String property) {
        return prop.getProperty(property);
    }

    public static void main(String args[]) {
        SybaseDBConnection.getConnection("ase");
    }

}

我写的这个测试用例

public class SybaseStatementTest {

    Connection conn;
    Statement stmt;

    // Setup methods
    @BeforeClass
    public void beforeClass() {
        conn = null;
        stmt = null;
    }

    // clean up method
    @AfterClass
    public void releaseResource() {
        if (stmt != null) {
            SybaseDBConnection.closeStatement(stmt);
        }
        if (conn != null) {
            SybaseDBConnection.closeConnection(conn);
        }
    }


    // Test case to check close statement using ASE database
    @Before
    public void openConnBeforerStmtTestASE() throws SQLException {
        conn = SybaseDBConnection.getConnection("ase");
        stmt = conn.createStatement();
    }

    @After
    public void closeConnAfterStmtTestASE() {
        if (conn != null) {
            SybaseDBConnection.closeConnection(conn);
        }
    }

    @Test
    public void testCloseStatementASE() {
        SybaseDBConnection.closeStatement(stmt);
        Assert.assertNull("Error occured", stmt);
    }
}

我收到初始化错误(@当我运行这个Junit测试用例时,BeforeClass应该是静态的。
请帮忙吗?

推荐答案

@BeforeClass @AfterClass 在类级而不是实例上运行,因此方法必须是静态的。

@BeforeClass and @AfterClass operate at the class level rather than on instances, so the methods need to be static.

您可以删除 beforeClass 方法。它没有做任何有用的事情,因为实例变量无论如何都将默认为null。并将 @AfterClass 更改为 @After

You can remove the beforeClass method. It is not doing anything useful because the instance variables will default to null anyway. And also change @AfterClass to @After.

测试本身不起作用,因为 SybaseDBConnection.closeStatement stmt 设置为null但是在方法外部不可见因为它是一个局部变量。

The test itself won't work because SybaseDBConnection.closeStatement is setting stmt to null but this is not visible outside the method because it's a local variable.

您可以编写如下测试:

public class SybaseStatementTest {
    Connection connection;

    @Before
    public void before() {
        connection = SybaseDBConnection.getConnection("ase");
    }

    @After
    public void after() {
        SybaseDBConnection.closeConnection(connection);
    }

    @Test
    public void closeStatementShouldCloseStatement() {
        Statement statement = connection.createStatement();
        SybaseDBConnection.closeStatement(statement);
        assertTrue(statement.isClosed());
    }

    @Test
    public void closeStatementWithNullShouldNotThrow() {
        SybaseDBConnection.closeStatement(null);
    }
}

这篇关于用于连接数据库的JUNIT测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆