Java 如何编写Junit测试用例来检查MethodCloseStatement?
包com.gs.sybase; 所有进口完成Java 如何编写Junit测试用例来检查MethodCloseStatement?,java,jdbc,junit,Java,Jdbc,Junit,包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 fi
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测试用例来测试closeStatement()方法
测试失败并显示
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)
junit.framework.AssertionFailedError:应为:但为:com.sybase.jdbc3.jdbc。SybStatement@6a13a848
位于junit.framework.Assert.fail(Assert.java:57)
位于junit.framework.Assert.assertTrue(Assert.java:22)
位于junit.framework.Assert.assertNull(Assert.java:277)
位于junit.framework.Assert.assertNull(Assert.java:268)
位于com.gs.test.SybaseDBConnectionTest.testCloseStatementASE(SybaseDBConnectionTest.java:94)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:47)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:44)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:309)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
请你解释一下原因好吗??据我所知,测试应该通过,因为stmt为空。Java是“按值传递”,这意味着如果方法参数为空,它将创建一个副本。为了传递对象,Java将引用复制到该对象,以便在方法中使用。因此,当您修改方法中的引用时(这是通过在SybaseDBConnection.closeStatement()中指定“null”来完成的),您实际上只修改引用的“副本”。
返回后,您将检查原始文件是否为空,但没有人接触原始文件。您已将conn varaiable创建为global i beleive。当您尝试在@beforemethod中创建连接时,您能否调试conn存储了什么值
conn = SybaseDBConnection.getConnection("iq");
我不确定封闭语句是否为null,因为您可以使用isClosed()测试语句-如果您关闭连接,可能会看到它为null,而不是语句。我要做的第一件事是使用调试器来查看您的
closeStatement
methodhmm是isClosed()中发生了什么测试再次失败,原因是代码无法访问。那么您能建议我如何测试它吗?实际上,Leo所说的assertTrue(stmt.isClosed())应该可以工作。但是,您正在测试驱动程序。我将模拟javax.sql.Statement,并通过验证stmt.close()是否已在模拟中被调用来检查类的行为。
conn = SybaseDBConnection.getConnection("iq");