Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ protected Statement createNewConnection() {
throw new ConnectionCreationException("Could create JDBC statement", e);
}
}

@Override
protected void closeConnectionQuietly(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
log.error("Could not close JDBC connection", e);
}
}

if (connection != null) {
try {
connection.close();
} catch (Exception e) {
log.error("Could not close JDBC connection", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.testcontainers.jdbc;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.Statement;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class ContainerLessJdbcDelegateTest {

@Test
void closeClosesStatementAndConnectionQuietly() throws Exception {
Connection connection = mock(Connection.class);
Statement statement = mock(Statement.class);
when(connection.createStatement()).thenReturn(statement);

ContainerLessJdbcDelegate delegate = new ContainerLessJdbcDelegate(connection);
delegate.execute("select 1", "test.sql", 1, false, false);

assertThatCode(delegate::close).doesNotThrowAnyException();

verify(statement).close();
verify(connection).close();
}
}