gpt4 book ai didi

java - 如何编写Junit testCase来检查方法closeStatement?

转载 作者:行者123 更新时间:2023-12-02 06:01:11 25 4
gpt4 key购买 nike

软件包 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 为空。

最佳答案

Java 是“按值传递”的,这意味着它会创建方法参数的副本。为了传递一个对象,Java 复制对该对象的引用以在方法中使用。因此,当您修改方法中的引用时(这就是通过在 SybaseDBConnection.closeStatement() 中分配“null”来完成的操作),您实际上只修改了引用的“副本”。返回后,你检查原件是否为空,但没有人碰过原件。

关于java - 如何编写Junit testCase来检查方法closeStatement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22666671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com