Интеграционное тестирование Dropwizard с Testcontainers

Я пытаюсь запустить интеграционные тесты 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())
    );

Итак, как я могу связать правила так, чтобы PostgreSQLContainer запускался первым, а контейнер запускался до создания DropwizardAppRule?


person user3960875    schedule 14.07.2017    source источник


Ответы (1)


Он заработал, запустив PostgreSQLContainer как singleton.

    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())
    );
person user3960875    schedule 02.08.2017