连接未关闭,即使调用已验证。

huangapple go评论72阅读模式
英文:

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来模拟一些对象的行为,例如OracleConnectionOracleCallableStatement等。测试的目标是验证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, &quot;parseSQLErrorFuntion&quot;, &quot;Some SQL error function&quot;);
		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: &lt;true&gt; but was: &lt;false&gt;

	}

}

答案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.

huangapple
  • 本文由 发表于 2020年9月25日 17:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64061336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定