在Dropwizard中以编程方式运行迁移

and*_*sem 6 java liquibase dropwizard

我有dropwizard-application(0.7.0),我想运行集成测试.

我使用DropwizardAppRule设置了集成测试,如下所示:

@ClassRule
public static final DropwizardAppRule<MyAppConfiguration> RULE =
        new DropwizardAppRule<MyAppConfiguration>(
                MyApplication.class, Resources.getResource("testconfiguration.yml").getPath());
Run Code Online (Sandbox Code Playgroud)

当我尝试使用它运行以下测试时,它不起作用,因为我没有运行我的迁移.运行迁移的最佳方法是什么?

测试:

@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"));
}
Run Code Online (Sandbox Code Playgroud)

组态:

 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;
}
Run Code Online (Sandbox Code Playgroud)

}

小智 8

感谢Kimble和andersem让我走上正轨.这是我在@BeforeClass方法中提出的:

// 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("");
    }
}
Run Code Online (Sandbox Code Playgroud)