如何编写一个Junit测试用例来检查方法close语句?

时间:2014-03-26 16:01:56

标签: java jdbc junit

package com.gs.sybase; 完成所有导入

public class SybaseDBConnection {

    private static Logger LOGGER = Logger.getLogger(SybaseDBConnection.class);

    public static Properties prop = null;

    // This block is responsible for loading the sybase.properties file
    static {
        try {
            prop = new Properties();
            prop.load(SybaseDBConnection.class.getClassLoader()
                    .getResourceAsStream("com/gs/properties/sybase.properties"));
        } catch (FileNotFoundException e) {
            System.err.println("Exception occured : FileNotFoundException : "
                    + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err
                    .println("Exception occured while loading the properties file");
            System.err.println("Exception occured : IOException : "
                    + e.getMessage());
            e.printStackTrace();
        }
    }

    /
    public static Connection getConnection(String databaseType)
            throws MigrationException {

        Connection conn = null;
        String driver = null;
        String url = null;
        String user = null;
        String password = null;

        try {

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

            SybDriver sybDriver = (SybDriver) Class.forName(driver)
                    .newInstance();
            sybDriver.setVersion(com.sybase.jdbcx.SybDriver.VERSION_LATEST);
            DriverManager.registerDriver(sybDriver);

            conn = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {
            LOGGER.error(
                    "Exception occured : SQLException : " + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        } catch (InstantiationException e) {
            LOGGER.error(
                    "Exception occured : InstantiationException : "
                            + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOGGER.error(
                    "Exception occured : IllegalAccessException : "
                            + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            LOGGER.error(
                    "Exception occured : ClassNotFoundException : "
                            + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        }

        return conn;
    }


    public static void closeConnection(Connection conn)
            throws MigrationException {
        try {
            if (null != conn) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            LOGGER.error(
                    "Exception occured : SQLException : " + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        }
    }


    public static void closeResultset(ResultSet rs) throws MigrationException {
        try {
            if (null != rs) {
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
            LOGGER.error(
                    "Exception occured : SQLException : " + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        }
    }


    public static void closePreparedStatement(PreparedStatement pstmt)
            throws MigrationException {
        try {
            if (null != pstmt) {
                pstmt.close();
                pstmt = null;
            }
        } catch (SQLException e) {
            LOGGER.error(
                    "Exception occured : SQLException : " + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        }
    }

    public static void closeStatement(Statement stmt) throws MigrationException {
        try {
            if (null != stmt) {
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            LOGGER.error(
                    "Exception occured : SQLException : " + e.getMessage(), e);
            throw new MigrationException(e.getMessage(), e);
        }
    }


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

}

对于相同的代码。我编写了一个Junit testCase来测试方法closeStatement()

@Before
public void openConnBeforeStmtTestASE() throws SQLException, MigrationException {
    conn = SybaseDBConnection.getConnection("iq");
    stmt = conn.createStatement();
}


@Test
public void testCloseStatementASE() throws SQLException, MigrationException {
SybaseDBConnection.closeStatement(stmt);
Assert.assertNull(stmt);
SybaseDBConnection.closeConnection(conn);

}

测试失败并显示

junit.framework.AssertionFailedError: Expected: <null> but was: com.sybase.jdbc3.jdbc.SybStatement@6a13a848
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.Assert.assertTrue(Assert.java:22)
    at junit.framework.Assert.assertNull(Assert.java:277)
    at junit.framework.Assert.assertNull(Assert.java:268)
    at com.gs.test.SybaseDBConnectionTest.testCloseStatementASE(SybaseDBConnectionTest.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

请你能解释一下这个原因吗?据我所知,测试应该通过,因为stmt为空。

2 个答案:

答案 0 :(得分:2)

Java是&#34;按值传递&#34;,这意味着它会在方法参数中创建副本。要传递Object,Java会复制对该对象的引用,以便在该方法中使用。因此,当您修改方法中的引用时(这是您通过在SybaseDBConnection.closeStatement()中分配&#39; null&#39;所做的那样),您实际上只修改了&#34; copy&#34;参考。 返回后,检查原件是否为空,但没有人触及原件。

答案 1 :(得分:0)

你已经创建了conn变量作为全局我相信所以你可以调试在尝试在@beforemethod中创建连接时存储的ct值是什么

                 conn = SybaseDBConnection.getConnection("iq");