Skip to content

Instantly share code, notes, and snippets.

@schaloner
Last active March 28, 2017 09:30
Show Gist options
  • Save schaloner/ec37bbf4053643f0e24888b9882b85eb to your computer and use it in GitHub Desktop.
Save schaloner/ec37bbf4053643f0e24888b9882b85eb to your computer and use it in GitHub Desktop.
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();
}
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();
}
}
public class FooDaoTest {
@Rule
public PostgreSQLContainer postgres = new PostgreSQLContainer();
@Before
public void setUp() {
// populate database
// postgres.getDriverClassName()
// postgres.getJdbcUrl()
// postgres.getUsername()
// postgres.getPassword()
}
}
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);
}
}
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