【发布时间】:2017-12-19 04:09:08
【问题描述】:
我正在尝试针对 dockered 数据库运行 dropwizard 的集成测试。
我尝试过的:
@ClassRule
public static final PostgreSQLContainer postgres = new PostgreSQLContainer();
@ClassRule
public final DropwizardAppRule<Configuration> RULE = new DropwizardAppRule<>(
Application.class,
CONFIG_PATH,
ConfigOverride.config("dataSourceFactory.url", postgres.getJdbcUrl()),
ConfigOverride.config("dataSourceFactory.user", postgres.getUsername()),
ConfigOverride.config("dataSourceFactory.password", postgres.getPassword())
);
我收到Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
将这些链接在一起也不起作用
@ClassRule
public static TestRule chain = RuleChain.outerRule(postgres = new PostgreSQLContainer())
.around(RULE = new DropwizardAppRule<>(
Application.class,
CONFIG_PATH,
ConfigOverride.config("dataSourceFactory.url", postgres.getJdbcUrl()),
ConfigOverride.config("dataSourceFactory.user", postgres.getUsername()),
ConfigOverride.config("dataSourceFactory.password", postgres.getPassword())
));
终于可以了,但据我了解,它为每个测试运行新的 DropwizardAppRule,这不好...
@ClassRule
public static final PostgreSQLContainer postgres = new PostgreSQLContainer();
@Rule
public final DropwizardAppRule<Configuration> RULE = new DropwizardAppRule<>(
Application.class,
CONFIG_PATH,
ConfigOverride.config("dataSourceFactory.url", postgres.getJdbcUrl()),
ConfigOverride.config("dataSourceFactory.user", postgres.getUsername()),
ConfigOverride.config("dataSourceFactory.password", postgres.getPassword())
);
那么我怎样才能在创建 DropwizardAppRule 之前链接规则,以便首先启动 PostgreSQLContainer 并启动容器?
【问题讨论】: