英文:
Connection not closed even if call is verified
问题
这段代码的含义是:
你能帮我理解这段代码吗?
我试图验证数据库连接是否已关闭。
即使使用Mocikto验证了close()方法是否被调用,连接仍然没有关闭。
我正在测试的代码(它在内部使用了con.close()):
public static void closeConnection(CallableStatement cs, Connection conn) {
JdbcUtils.closeStatement(cs);
JdbcUtils.closeConnection(conn);
}
以及测试部分:
public class DataProviderTest {
@InjectMocks
DataProvider dataProvider;
@Mock
OracleConnection oracleConnection;
@Mock
DatabaseMetaData databaseMetadata;
@Mock
PoolDataSource dataSource;
@Mock
OracleCallableStatement oracleCallableStatement;
@BeforeEach
public void setUp() throws SQLException {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(dataProvider, "parseSQLErrorFuntion", "Some SQL error function");
Mockito.when(dataSource.getConnection()).thenReturn(oracleConnection);
Mockito.when(oracleConnection.getMetaData()).thenReturn(databaseMetadata);
Mockito.when(oracleConnection.prepareCall(Mockito.anyString())).thenReturn(oracleCallableStatement);
}
@Test
public void closeConnectionTest() throws SQLException {
OracleConnection connection = dataProvider.getConnection();
OracleCallableStatement statement = oracleCallableStatement;
assertFalse(connection.isClosed());
dataProvider.closeConnection(statement, connection);
Mockito.verify(connection, Mockito.atLeast(1)).close(); //pass
Mockito.verify(statement, Mockito.atLeast(1)).close(); //pass
assertTrue(connection.isClosed()); // expected: <true> but was: <false>
}
}
请注意,这是一个Java测试类,旨在测试一个名为DataProvider
的类的方法。测试的重点在于closeConnectionTest
方法,该方法测试了closeConnection
方法的行为。在测试过程中,它使用了Mockito来模拟一些对象的行为,例如OracleConnection
和OracleCallableStatement
等。测试的目标是验证closeConnection
方法是否能够成功关闭数据库连接,并且连接确实已关闭。最后一个断言检查连接是否关闭,但它实际上失败了。
英文:
Can you help me understand this piece of code?
I am trying to verify that database connection is closed.
Even if Mocikto verifies that close() method were called connection is not closed.
Code I am testing (it uses inside con.close()):
public static void closeConnection(CallableStatement cs, Connection conn) {
JdbcUtils.closeStatement(cs);
JdbcUtils.closeConnection(conn);
}
And test:
public class DataProviderTest {
@InjectMocks
DataProvider dataProvider;
@Mock
OracleConnection oracleConnection;
@Mock
DatabaseMetaData databaseMetadata;
@Mock
PoolDataSource dataSource;
@Mock
OracleCallableStatement oracleCallableStatement;
@BeforeEach
public void setUp() throws SQLException {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(dataProvider, "parseSQLErrorFuntion", "Some SQL error function");
Mockito.when(dataSource.getConnection()).thenReturn(oracleConnection);
Mockito.when(oracleConnection.getMetaData()).thenReturn(databaseMetadata);
Mockito.when(oracleConnection.prepareCall(Mockito.anyString())).thenReturn(oracleCallableStatement);
}
@Test
public void closeConnectionTest() throws SQLException {
OracleConnection connection = dataProvider.getConnection();
OracleCallableStatement statement = oracleCallableStatement;
assertFalse(connection.isClosed());
dataProvider.closeConnection(statement, connection);
Mockito.verify(connection, Mockito.atLeast(1)).close(); //pass
Mockito.verify(statement, Mockito.atLeast(1)).close(); //pass
assertTrue(connection.isClosed()); // expected: <true> but was: <false>
}
}
答案1
得分: 1
你的测试中的连接是一个模拟对象,因此运行close方法实际上不会将isClosed的值从false更改为true。
你可以移除最后一个断言,因为它是一个模拟对象,你只需要验证连接上是否执行了close方法,无需验证它执行了什么操作。
英文:
The connection in your test is a mocked object, therefor running the close method does not actually change the value of isClosed from false to true.
You can remove the last assert, since it's a mock, all you need to verify is that the close method was executed on the connection, you don't need to verify what it did.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论