Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard integration testing with Testcontainers

I'm trying to run dropwizard's integration tests against a dockered database.

  • Dropwizard
  • Testcontainers

What I've tried:

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

I get Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started

Chaining these together does not work either

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

Finally this works, but as I understand it runs the new DropwizardAppRule for every test and this is not good...

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

So how can I chain the rules such that PostgreSQLContainer is initiated first and container has started before creating the DropwizardAppRule?

like image 558
user3960875 Avatar asked Jul 14 '17 06:07

user3960875


1 Answers

Got it working by initiating PostgreSQLContainer as 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())
    );
like image 84
user3960875 Avatar answered Oct 31 '22 07:10

user3960875