PostgresConnection implements IStorageConnection { public boolean validate() throws StorageException { try { if (connection.isClosed()) { return false; } try { validationQueryStatement.executeQuery(); } catch (SQLException e) { try { rollback(); validationQueryStatement.executeQuery(); } catch (SQLException ee) { e.addSuppressed(ee); throw e; } } return true; } catch (SQLException e) { throw new StorageException("Error validating connection: ", e); } } PostgresConnection(final ConnectionOptions options); ICompiledQuery compileQuery(final IPreparedQuery preparedQuery); boolean validate(); void close(); void commit(); void rollback();  }
@Test public void MustCorrectValidate() throws SQLException, StorageException { when(connection.isClosed()).thenReturn(false); assertTrue(testPostgresConnection.validate()); verify(connection).isClosed(); verify(validateStatement).executeQuery(); }
@Test public void MustCorrectValidateWhenIsClosedTrue() throws SQLException, StorageException { when(connection.isClosed()).thenReturn(true); assertTrue(!testPostgresConnection.validate()); verify(connection).isClosed(); }
@Test public void MustInCorrectValidateWhenIsClosedThrowException() throws SQLException, StorageException { when(connection.isClosed()).thenThrow(new SQLException()); try { testPostgresConnection.validate(); } catch (StorageException e) { verify(connection).isClosed(); return; } assertTrue("Must throw exception", false); }