Spring测试想要连接数据库

R. *_*ito 6 spring unit-testing jpa spring-boot mockmvc

我正在尝试为我的应用程序编写一些测试并遇到以下问题:我定义了一个包含以下内容的 application-test.yml :

server:
  port: 8085

spring:
    security:
        oauth2:
            resourceserver:
                jwt:
                    # change localhost:8081 with container name
                    issuer-uri: http://localhost:8081/auth/realms/drivingschool
                    jwk-set-uri: http://localhost:8081/auth/realms/drivingschool/protocol/openid-connect/certs

keycloak:
    realm: drivingschool
    auth-server-url: http://localhost:8081/auth
    ssl-required: external
    resource: client-interface
    use-resource-role-mappings: true
    credentials:
        secret: xxx
    bearer-only: true
Run Code Online (Sandbox Code Playgroud)

我的测试课:

server:
  port: 8085

spring:
    security:
        oauth2:
            resourceserver:
                jwt:
                    # change localhost:8081 with container name
                    issuer-uri: http://localhost:8081/auth/realms/drivingschool
                    jwk-set-uri: http://localhost:8081/auth/realms/drivingschool/protocol/openid-connect/certs

keycloak:
    realm: drivingschool
    auth-server-url: http://localhost:8081/auth
    ssl-required: external
    resource: client-interface
    use-resource-role-mappings: true
    credentials:
        secret: xxx
    bearer-only: true
Run Code Online (Sandbox Code Playgroud)

测试全部通过绿色,但在日志中我可以看到,我的应用程序尝试连接到我的(基本)application.yml 中配置的数据库。

ava.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=9005)(type=master) : Socket fail to connect to host:localhost, port:9005. Verbindungsaufbau abgelehnt (Connection refused)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73) ~[mariadb-java-client-2.6.1.jar:na]
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:192) ~[mariadb-java-client-2.6.1.jar:na]
Run Code Online (Sandbox Code Playgroud)
jpa:
      database-platform: org.hibernate.dialect.MariaDBDialect
      hibernate:
        use-new-id-generator-mappings: false
        ddl-auto: create

    datasource:
      url: jdbc:mariadb://localhost:9005/waterloo
      username: waterloo
      password: xxx
      driver-class-name: org.mariadb.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

创建 application-prod.yml 并将所有内容从 application.yml 移动到 application-prod.yml 时,它告诉我必须配置数据源 URL

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
Run Code Online (Sandbox Code Playgroud)

我有以下问题:

  1. application.yml 文件是否分层(*-test.yml 设置位于 application.yml 之上)?
  2. 当我没有在 application-test.yml 上设置数据源并在测试中模拟存储库时,为什么 Spring 会尝试建立到我的数据库的连接?
  3. Spring在这部分尝试建立连接是否正常?3.1)如果不是:我如何防止它这样做?

谢谢并致以亲切的问候!

R. *_*ito 2

遗漏了以下注释:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)