Run migrations programmatically in Dropwizard
dropwizard, java, liquibase
Solution
Thanks to Kimble and andersem for putting me on the right track. Here's what I came up with in my @BeforeClass method:
// Create the test database with the LiquiBase migrations.
@BeforeClass
public static void up() throws Exception
{
ManagedDataSource ds = RULE.getConfiguration().getMainDataSource().build(
RULE.getEnvironment().metrics(), "migrations");
try (Connection connection = ds.getConnection())
{
Liquibase migrator = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(), new JdbcConnection(connection));
migrator.update("");
}
}
Problem
I have dropwizard-application (0.7.0) for which I want to run integration tests. I've set up an integration test using DropwizardAppRule, like this: ``` @ClassRule public static final DropwizardAppRule<MyAppConfiguration> RULE = new DropwizardAppRule<MyAppConfiguration>( MyApplication.class, Resources.getResource("testconfiguration.yml").getPath()); ``` When I try to run the below tests using it, it doesn't work because I haven't run my migrations. What is the best way to run the migrations? Test: ``` @Test public void fooTest() { Client client = new Client(); String root = String.format("http://localhost:%d/", RULE.getLocalPort()); URI uri = UriBuilder.fromUri(root).path("/users").build(); client.resource(uri).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(User.class, new LoginUserDTO("email@email.com", "password")); } ``` Configuration: ``` public class MyAppConfiguration extends Configuration { @Valid @NotNull private DataSourceFactory database = new DataSourceFactory(); @JsonProperty("database") public DataSourceFactory getDataSourceFactory() { return database; } @JsonProperty("database") public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { this.database = dataSourceFactory; } ``` }