Last active
March 28, 2017 09:30
-
-
Save schaloner/ec37bbf4053643f0e24888b9882b85eb to your computer and use it in GitHub Desktop.
Code samples for a blog post at http://www.objectify.be/wordpress/2017/03/28/database-testing-with-testcontainers/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.objectify.tcexample; | |
import org.junit.Test; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public abstract AbstractUserDaoTest { | |
@Test | |
public void testFoo() { | |
assertThat(dao().something()).isEqualTo(whatever); | |
} | |
// many, many more tests | |
public abstract UserDao dao(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.objectify.tcexample.db; | |
import com.google.common.collect.ImmutableMap; | |
import org.testcontainers.containers.PostgreSQLContainer; | |
import play.db.Database; | |
import play.db.Databases; | |
import play.db.evolutions.Evolutions; | |
public interface DbTestSupport { | |
default Database create(final PostgreSQLContainer postgres) throws Exception { | |
final Database database = Databases.createFrom("default", | |
postgres.getDriverClassName(), | |
postgres.getJdbcUrl(), | |
ImmutableMap.of("username", postgres.getUsername(), | |
"password", postgres.getPassword())); | |
Evolutions.applyEvolutions(database); | |
return database; | |
} | |
default void destroy(final Database database) { | |
Evolutions.cleanupEvolutions(database); | |
database.shutdown(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooDaoTest { | |
@Rule | |
public PostgreSQLContainer postgres = new PostgreSQLContainer(); | |
@Before | |
public void setUp() { | |
// populate database | |
// postgres.getDriverClassName() | |
// postgres.getJdbcUrl() | |
// postgres.getUsername() | |
// postgres.getPassword() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.objectify.tcexample.db; | |
import be.objectify.tcexample.AbstractUserDaoTest; | |
import be.objectify.tcexample.UserDao; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.testcontainers.containers.PostgreSQLContainer; | |
import play.db.Database; | |
public class JooqUserDaoTest extends AbstractUserDaoTest implements DbTestSupport, | |
TestData { | |
@Rule | |
public PostgreSQLContainer postgres = new PostgreSQLContainer(); | |
private Database database; | |
@Before | |
public void setup() throws Exception { | |
// the database has all evolutions applied | |
database = create(postgres); | |
// load some test data | |
loadTestData(database); | |
} | |
@After | |
public void tearDown() { | |
destroy(database); | |
} | |
@Override | |
public UserDao dao() { | |
return new JooqUserDao(database); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.objectify.tcexample.db; | |
import org.jooq.impl.DSL; | |
import play.db.Database; | |
import java.sql.Connection; | |
import java.sql.Timestamp; | |
import java.time.Instant; | |
import static be.objectify.tcexample.db.jooq.generated.Tables.ACCOUNT; | |
public interface TestData { | |
default void loadTestData(Database database) { | |
database.withConnection((Connection conn) -> { | |
DSL.using(conn) | |
.insertInto(ACCOUNT, | |
ACCOUNT.ID, | |
ACCOUNT.KEY, | |
ACCOUNT.CREATED_ON) | |
.values(1, | |
"test-account-a", | |
Timestamp.from(Instant.now())) | |
.execute(); | |
DSL.using(conn) | |
.insertInto(ACCOUNT, | |
ACCOUNT.ID, | |
ACCOUNT.KEY, | |
ACCOUNT.CREATED_ON) | |
.values(2, | |
"test-account-b", | |
Timestamp.from(Instant.now())) | |
.execute(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment