微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用嵌入式内存 postgresql 数据库进行 Java Spring 启动单元测试

如何解决使用嵌入式内存 postgresql 数据库进行 Java Spring 启动单元测试

我有一个 Java Spring 启动项目,大量使用数据库 (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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。