首页 > 解决方案 > 使用嵌入式内存 postgresql DB 的 Java Spring 引导单元测试

问题描述

我有一个 Java Spring boot 项目,大量使用数据库(Postgres)作为它的存储库/数据。它是基本的 MVC 项目,控制器都是 REST 控制器。该项目运行良好(服务已启动,能够通过 REST 客户端和所有客户端调用服务)。

现在,我正在向它添加单元测试。我对 Spring Boot 很陌生,主要是单元测试部分。由于 CI/CD(构建管道),我无法使用持久/外部数据库进行测试。因此我需要使用内存数据库。

初始运行(主类)运行一堆数据库查询以在项目启动时建立缓存。所以我需要 postgres DB 进行测试(使用了很多 DB 函数)。

我正在浏览https://github.com/opentable/otj-pg-embedded这似乎是不错的候选人。

我正在尝试编写如下的基本测试,但它失败了。

@ExtendWith(SpringExtension.class)
@FlywayTest
@DataJpaTest
@ActiveProfiles("test")
@AutoConfigureEmbeddedDatabase
public class AuthControllerTest {

    @Autowired
    private AuthController authController;

    @Test
    void contextLoads() {

    }

    @Test
    public void authenticateUserTest() {
    ...
    }
}

我的测试文件夹如下:

test
    |
    resources
            |
            application-test.properties
            db
              |
              migration
                     |
                     V1__baseline.sql
                     V2__static_data.sql
                     V3__changeset.sql

在 application-test.properties 文件中,我有以下属性:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database=POSTGRESQL
spring.flyway.schemas=test
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

但是,当我为 Docker 运行测试失败时?为什么它会搜索和使用 docker?

2021-06-08 14:56:36.883  INFO 12748 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-06-08 14:56:36.886  INFO 12748 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in LAZY mode.
2021-06-08 14:56:37.429  INFO 12748 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 527ms. Found 23 JPA repository interfaces.
2021-06-08 14:56:37.659  INFO 12748 --- [           main] EmbeddedDatabaseContextCustomizerFactory : Replacing 'dataSource' DataSource bean with embedded version
2021-06-08 14:56:38.138  INFO 12748 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'io.zonky.test.db.config.EmbeddedDatabaseAutoConfiguration' of type [io.zonky.test.db.config.EmbeddedDatabaseAutoConfiguration$$EnhancerBySpringCGLIB$$bddef8e9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-06-08 14:56:38.665  INFO 12748 --- [           main] eProvider$PriorityThreadPoolTaskExecutor : Initializing ExecutorService
2021-06-08 14:56:39.822  WARN 12748 --- [  prefetching-1] o.t.utility.TestcontainersConfiguration  : Attempted to read Testcontainers configuration file at file:/C:/Users/username/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: C:\Users\username\.testcontainers.properties (The system cannot find the file specified)
2021-06-08 14:56:39.847  INFO 12748 --- [  prefetching-1] .t.d.DockerMachineClientProviderStrategy : docker-machine executable was not found on PATH
2021-06-08 14:57:10.598 ERROR 12748 --- [  prefetching-1] o.t.d.DockerClientProviderStrategy       : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2021-06-08 14:57:10.598 ERROR 12748 --- [  prefetching-1] o.t.d.DockerClientProviderStrategy       :     NpipeSocketClientProviderStrategy: failed with exception TimeoutException (Timeout waiting for result with exception). Root cause NoSuchFileException (\\.\pipe\docker_engine)
2021-06-08 14:57:10.598 ERROR 12748 --- [  prefetching-1] o.t.d.DockerClientProviderStrategy       : As no valid configuration was found, execution cannot continue
2021-06-08 14:57:10.795  INFO 12748 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-08 14:57:10.937  INFO 12748 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.20.Final
2021-06-08 14:57:11.320  INFO 12748 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2021-06-08 14:57:11.583  WARN 12748 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : io.zonky.test.db.provider.ProviderException: Unexpected error when preparing a database cluster; nested exception is java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
.
.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is io.zonky.test.db.shaded.com.google.common.util.concurrent.UncheckedExecutionException: io.zonky.test.db.provider.ProviderException: Unexpected error when preparing a database cluster; nested exception is java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

标签: javaspring-bootjunitin-memory-database

解决方案


推荐阅读