【问题标题】:Dropwizard integration testing with Testcontainers使用 Testcontainers 进行 Dropwizard 集成测试
【发布时间】: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 并启动容器?

【问题讨论】:

    标签: dropwizard testcontainers


    【解决方案1】:

    通过将 PostgreSQL Container 作为单例启动来使其工作。

        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())
        );
    

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 2015-05-06
      • 2019-09-06
      • 2021-05-02
      • 2022-10-21
      • 2019-09-19
      • 2020-04-02
      • 2015-02-12
      相关资源
      最近更新 更多