用于 Spring Boot 测试的嵌入式 Postgres

新手上路,请多包涵

我正在构建一个由 Postgres 支持的 Spring Boot 应用程序,使用 Flyway 进行数据库迁移。我一直遇到无法在 Postgres 和嵌入式单元测试数据库(即使启用了 Postgres 兼容模式)中生成所需结果的迁移的问题。所以我正在考虑使用嵌入式 Postgres 进行单元测试。

我遇到了 一个看起来很有前途的嵌入式 postgres 实现,但并没有真正了解如何将其设置为仅在 Spring Boot 的单元测试框架中运行(用于测试 Spring Data 存储库)。如何使用上述工具或 Postgres 的替代嵌入式版本进行设置?

原文由 SingleShot 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 775
2 个回答

我是@MartinVolejnik 提到的 Embedded-database-spring-test 库的作者。我认为该库应该满足您的所有需求(PostgreSQL + Spring Boot + Flyway + 集成测试)。很抱歉给您带来了麻烦,所以我创建了一个 简单的演示应用程序,演示了该库与 Spring Boot 框架的使用。下面我总结了一些你需要做的基本步骤。

Maven配置

添加以下 Maven 依赖项:

 <dependency>
    <groupId>io.zonky.test</groupId>
    <artifactId>embedded-database-spring-test</artifactId>
    <version>2.0.1</version>
    <scope>test</scope>
</dependency>

飞路配置

将以下属性添加到您的应用程序配置中:

 # Sets the schemas managed by Flyway -> change the xxx value to the name of your schema
# flyway.schemas=xxx // for spring boot 1.x.x
spring.flyway.schemas=xxx // for spring boot 2.x.x

此外,请确保您不使用 org.flywaydb.test.junit.FlywayTestExecutionListener 。因为库有自己的测试执行侦听器,可以优化数据库初始化,如果应用 FlywayTestExecutionListener ,这种优化没有效果。

例子

演示嵌入式数据库使用的测试类示例:

 @RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureEmbeddedDatabase
public class SpringDataJpaAnnotationTest {

    @Autowired
    private PersonRepository personRepository;

    @Test
    public void testEmbeddedDatabase() {
        Optional<Person> personOptional = personRepository.findById(1L);

        assertThat(personOptional).hasValueSatisfying(person -> {
            assertThat(person.getId()).isNotNull();
            assertThat(person.getFirstName()).isEqualTo("Dave");
            assertThat(person.getLastName()).isEqualTo("Syer");
        });
    }
}

原文由 Tomáš Vaněk 发布,翻译遵循 CC BY-SA 4.0 许可协议

看看这个: https ://github.com/zonkyio/embedded-database-spring-test。需要明确的是,它用于集成测试。这意味着 Spring 上下文在单个测试期间被初始化。

根据工具文档,您需要做的就是在类上方放置 @AutoConfigureEmbeddedDatabase 注释:

 @RunWith(SpringRunner.class)
@AutoConfigureEmbeddedDatabase
@ContextConfiguration("/path/to/app-config.xml")
public class FlywayMigrationIntegrationTest {

    @Test
    @FlywayTest(locationsForMigrate = "test/db/migration")
    public void testMethod() {
        // method body...
    }
}

并添加 Maven 依赖项:

 <dependency>
  <groupId>io.zonky.test</groupId>
  <artifactId>embedded-database-spring-test</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
</dependency>

要将它与 @DataJpaTest 一起使用,您需要使用注释 @AutoConfigureTestDatabase(replace = NONE) 禁用默认测试数据库:

 @RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = NONE)
@AutoConfigureEmbeddedDatabase
@DataJpaTest
public class SpringDataJpaTest {
// class body...
}

为了使使用更舒适,您还可以创建一个复合注释,例如:

 @Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@AutoConfigureTestDatabase(replace = NONE)
@AutoConfigureEmbeddedDatabase
@DataJpaTest
public @interface PostgresDataJpaTest {
}

..然后在您的测试类上方使用它:

 @RunWith(SpringRunner.class)
@PostgresDataJpaTest // custom composite annotation
public class SpringDataJpaTest {
// class body...
}

原文由 Martin Volejnik 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题