Connection not closed even if call is verified

Mateusz Gebroski :

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>

    }

}
Nir Levy :

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring Data JPA Closed Connection

分類Dev

The underlying connection was closed - WEB SERVICES

分類Dev

Server sent events connection closed or data not received

分類Dev

When Entity Framework Connection Gets Open & Closed

分類Dev

How to keep an IntentService running even when app is closed?

分類Dev

Getting connection returns false even when the device has connection

分類Dev

Error: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed

分類Dev

Starting docker container results in docker-credential-secretservice: The connection is closed

分類Dev

Connection Pool Closed when using Spark-MongoDB Connector

分類Dev

Client prints Half Data Before Connection is Closed By Server on Socket

分類Dev

Getting FTP working on an Azure VM, overcoming 'connection closed' on ftp client

分類Dev

Python Socket - Why connection closed after sending several messages?

分類Dev

LINQ TO SQL in WPF Application generating connection is closed exception, sometimes

分類Dev

Upgrading from MySQL 5.5 to 5.6 and getting "SQLException: Connection is closed"

分類Dev

Connection state is closed when sending authorization request using DDP protocol

分類Dev

Spring app throwing java.sql.SQLRecoverableException: Closed Connection

分類Dev

Error: Object reference not set to an instance of an object./The connection was not closed. The connection's current state is open

分類Dev

Is SSL secure connection available without browser call?

分類Dev

Getting bind: address already in use even after closing the connection in golang

分類Dev

Python continue looping over a list even when a connection error occurs

分類Dev

load multiple audio files to be played simultaneously even with poor internet connection

分類Dev

Is it possible to have a count down timer continue running even after the user has closed the app

分類Dev

VLC is still running even I closed it already. How to I close it?

分類Dev

Node.jsサーバー+ SSL = ERR_CONNECTION_CLOSED

分類Dev

Reading from a TcpStream with Read::read_to_string hangs until the connection is closed by the remote end

分類Dev

接続確立でのWebSocketエラー:net :: ERR_CONNECTION_CLOSED

分類Dev

SSHKit::Runner::ExecuteError: Exception while executing on host xx.xx.xx.xx: connection closed by remote host

分類Dev

connection.closedは関数SignalRではありません

分類Dev

sharepoint powershell - Exception calling "ExecuteQuery" with "0" argument(s): "The underlying connection was closed:"

Related 関連記事

  1. 1

    Spring Data JPA Closed Connection

  2. 2

    The underlying connection was closed - WEB SERVICES

  3. 3

    Server sent events connection closed or data not received

  4. 4

    When Entity Framework Connection Gets Open & Closed

  5. 5

    How to keep an IntentService running even when app is closed?

  6. 6

    Getting connection returns false even when the device has connection

  7. 7

    Error: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed

  8. 8

    Starting docker container results in docker-credential-secretservice: The connection is closed

  9. 9

    Connection Pool Closed when using Spark-MongoDB Connector

  10. 10

    Client prints Half Data Before Connection is Closed By Server on Socket

  11. 11

    Getting FTP working on an Azure VM, overcoming 'connection closed' on ftp client

  12. 12

    Python Socket - Why connection closed after sending several messages?

  13. 13

    LINQ TO SQL in WPF Application generating connection is closed exception, sometimes

  14. 14

    Upgrading from MySQL 5.5 to 5.6 and getting "SQLException: Connection is closed"

  15. 15

    Connection state is closed when sending authorization request using DDP protocol

  16. 16

    Spring app throwing java.sql.SQLRecoverableException: Closed Connection

  17. 17

    Error: Object reference not set to an instance of an object./The connection was not closed. The connection's current state is open

  18. 18

    Is SSL secure connection available without browser call?

  19. 19

    Getting bind: address already in use even after closing the connection in golang

  20. 20

    Python continue looping over a list even when a connection error occurs

  21. 21

    load multiple audio files to be played simultaneously even with poor internet connection

  22. 22

    Is it possible to have a count down timer continue running even after the user has closed the app

  23. 23

    VLC is still running even I closed it already. How to I close it?

  24. 24

    Node.jsサーバー+ SSL = ERR_CONNECTION_CLOSED

  25. 25

    Reading from a TcpStream with Read::read_to_string hangs until the connection is closed by the remote end

  26. 26

    接続確立でのWebSocketエラー:net :: ERR_CONNECTION_CLOSED

  27. 27

    SSHKit::Runner::ExecuteError: Exception while executing on host xx.xx.xx.xx: connection closed by remote host

  28. 28

    connection.closedは関数SignalRではありません

  29. 29

    sharepoint powershell - Exception calling "ExecuteQuery" with "0" argument(s): "The underlying connection was closed:"

ホットタグ

アーカイブ