首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Testcontainers进行Dropwizard集成测试

用Testcontainers进行Dropwizard集成测试
EN

Stack Overflow用户
提问于 2017-07-14 06:51:07
回答 1查看 2.4K关注 0票数 9

我正在尝试在dockered数据库上运行下拉向导的集成测试。

我试过的是:

代码语言:javascript
复制
@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

把这些链接在一起也不管用。

代码语言:javascript
复制
@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,这是不好的.

代码语言:javascript
复制
@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之前已经启动?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-02 06:55:19

通过启动作为单例的PostgreSQLContainer来实现它。

代码语言:javascript
复制
    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())
    );
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45096498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档