我正在尝试在dockered数据库上运行下拉向导的集成测试。
我试过的是:
@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())
);那么,我如何链接规则,使PostgreSQLContainer首先启动,容器在创建DropwizardAppRule之前已经启动?
发布于 2017-08-02 06:55:19
通过启动作为单例的PostgreSQLContainer来实现它。
public static final PostgreSQLContainer postgres = new PostgreSQLContainer();
static {
postgres.start();
}
@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())
);https://stackoverflow.com/questions/45096498
复制相似问题