Last active
March 20, 2020 23:01
-
-
Save tzachz/81038a9f70dcb3dca8cd to your computer and use it in GitHub Desktop.
Dropwizard JDBI JUnit Rule using H2 by default - based on https://gist.github.com/yunspace/9a50e11dbd8661271220
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
import com.codahale.metrics.MetricRegistry; | |
import io.dropwizard.db.DataSourceFactory; | |
import io.dropwizard.jackson.Jackson; | |
import io.dropwizard.jdbi.DBIFactory; | |
import io.dropwizard.setup.Environment; | |
import liquibase.Liquibase; | |
import liquibase.database.jvm.JdbcConnection; | |
import liquibase.exception.LiquibaseException; | |
import liquibase.resource.ClassLoaderResourceAccessor; | |
import org.junit.rules.ExternalResource; | |
import org.skife.jdbi.v2.DBI; | |
import org.skife.jdbi.v2.Handle; | |
public class H2JDBIRule extends ExternalResource { | |
private DBI dbi; | |
private Handle handle; | |
private Liquibase liquibase; | |
public DBI getDbi() { | |
return dbi; | |
} | |
@Override | |
protected void before() throws Throwable { | |
Environment environment = new Environment("test-env", Jackson.newObjectMapper(), null, new MetricRegistry(), null); | |
dbi = new DBIFactory().build(environment, getDataSourceFactory(), "test"); | |
handle = dbi.open(); | |
migrateDatabase(); | |
} | |
@Override | |
protected void after() { | |
try { | |
liquibase.dropAll(); | |
} catch (Exception e) { | |
throw new RuntimeException("failed clearing up Liquibase object", e); | |
} | |
handle.close(); | |
} | |
private void migrateDatabase() throws LiquibaseException { | |
liquibase = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(), new JdbcConnection(handle.getConnection())); | |
liquibase.update(""); | |
} | |
private DataSourceFactory getDataSourceFactory() { | |
DataSourceFactory dataSourceFactory = new DataSourceFactory(); | |
dataSourceFactory.setDriverClass("org.h2.Driver"); | |
dataSourceFactory.setUrl("jdbc:h2:./build/h2db"); | |
dataSourceFactory.setUser("sa"); | |
dataSourceFactory.setPassword("sa"); | |
return dataSourceFactory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment