当前位置: 首页 > 知识库问答 >
问题:

Dropwizard与Testcontainers集成测试

国阳
2023-03-14

我正在尝试对停靠数据库运行DropWizard的集成测试。

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

我得到由:java.lang.IllegalStateException引起的:映射端口只能在容器启动后获得

将这些链接在一起也不起作用

@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首先启动,并且container在创建DropWizardApprule之前已经启动?

共有1个答案

陆建木
2023-03-14

通过将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())
    );
 类似资料:
  • 我是Dropwizard的新手。我想在我的项目中实现AWS S3文件上传服务。 我没有收到任何通过拖放在AWS S3上上传文件的教程。 我在pom.xml中添加了以下依赖项 我已经注册了多部分功能。将应用程序类的run()方法中的- 然后在资源类中定义方法为- 但在运行时,它显示以下日志:- 如何获取上传文件的url?如何通过编码检查文件上传?我遗漏了什么吗?有人知道这件事吗。如果dropwiza

  • 第一:是的,我读到了https://dropwizard.github.io/dropwizard/manual/testing.html 我想做一些集成测试,以及为什么我必须启动整个应用程序。现在的问题是,我有一些与“外部世界”的接口,如DB或一个内部的Rest-Client,它与一个远程应用程序交谈。我想用嘲弄来嘲笑他们。通常这没问题。 现在我的问题是:如何使用模拟数据库和模拟客户端启动整个应

  • 当我运行命令gradle war integrationTest时,集成测试失败,因为它在服务项目中找不到上下文侦听器。错误消息的片段如下: 对此我们非常感谢您的帮助。提前致谢

  • 简介:我们的产品需要对3个不同的数据库进行集成测试: 甲骨文 博士后 MSSQL 我们使用Spring Boot作为我们的框架和TestContainers来启动上面提到的数据库。 问题是:我们需要对每个容器(数据库)运行相同的测试。 在网上挖掘了很多之后,我能想到的唯一方法是使用一个BaseClass,在那里我们编写所有的测试用例和每个容器,我们创建一个继承自BaseClass的类,我们覆盖这个

  • 我需要使用Postgres、Redis和Elasticsearch在Spring Boot项目中设置集成测试(稍后还会添加Kafka)。到目前为止,我找到了两种选择: H2数据库和嵌入式Redis。这可能行得通,但我们在一些查询中使用了一些Postgres特定的函数,因此这不允许涵盖所有情况 第三个选项需要一些手工工作。绝对有可能制作一个包含本地使用测试所需的所有服务的docker组合文件,并让架

  • 问题内容: 我使用spock编写测试用例,使用jenkins运行和发布我的测试用例。我能够得到报告的代码覆盖率,但是声纳 仅 向我显示 Java 单元测试用例;在 常规测试案例是完全缺失 以下pom.xml用作参考 https://github.com/kkapelon/java-testing-with- spock/blob/master/chapter7/spring-standalone-