Skip to content

Instantly share code, notes, and snippets.

@victornoel
Last active March 25, 2017 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victornoel/bef5ac4e037949594210b8ccf5779d6d to your computer and use it in GitHub Desktop.
Save victornoel/bef5ac4e037949594210b8ccf5779d6d to your computer and use it in GitHub Desktop.
import static org.assertj.core.api.Assertions.assertThat;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;
import org.zapodot.junit.db.plugin.LiquibaseInitializer;
public class EmbeddedDbBugTest {
@Rule
public final EmbeddedDatabaseRule dbRule = EmbeddedDatabaseRule.builder()
.initializedByPlugin(LiquibaseInitializer.builder().withChangelogResource("migrations.xml").build())
.build();
private void insert(final Connection conn) throws SQLException {
try (final Connection connection = conn; final Statement statement = connection.createStatement()) {
final int result = statement.executeUpdate("INSERT INTO TEST(id) VALUES (1)");
assertThat(result).isEqualTo(1);
}
}
private void select(final Connection conn) throws SQLException {
try (final Connection connection = conn;
final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery("SELECT * from TEST")) {
// one and only one result
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.next()).isFalse();
}
}
@Test
public void failsButShouldnt() throws Exception {
insert(dbRule.getConnection());
select(DriverManager.getConnection(dbRule.getConnectionJdbcUrl()));
}
@Test
public void doesntFailAsExpected() throws Exception {
insert(dbRule.getConnection());
select(dbRule.getConnection());
}
@Test
public void doesntFailAsExpected2() throws Exception {
insert(DriverManager.getConnection(dbRule.getConnectionJdbcUrl()));
select(DriverManager.getConnection(dbRule.getConnectionJdbcUrl()));
}
@Test
public void doesntFailAsExpected3() throws Exception {
insert(DriverManager.getConnection(dbRule.getConnectionJdbcUrl()));
select(dbRule.getConnection());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="1" author="vnoel">
<createTable tableName="test">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment