gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:47:05 26 4
gpt4 key购买 nike

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

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

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

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

测试:

@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"));
}

配置:

 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;
}

最佳答案

感谢 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("");
}
}

关于java - 在 Dropwizard 中以编程方式运行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23579607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com