首页 > 解决方案 > 在执行 Spring Boot App 期间测试 application.properties 加载,而不是 main/application.prop

问题描述

我已经通过 Spring Boot 应用程序连接了 MARIA DB (Mysql)(在 src/main/resources/aplication.properties 中给出了 mariadb 的详细信息)

我写了一些基本的测试。在 test/resources/application.properties --> 给出 H2 数据库详细信息。

如果我正在运行测试,它会正确获取 test/resources/application.properties 并连接到 H2 Db 并仅在 H2 中执行测试。

但是当我运行我的 Spring Boot 应用程序时,它也会获取 H2 详细信息。(而不是 MariaDB)

如果我要删除 test/resources/application.properties 然后运行我的 spring boot 应用程序-> 它正在获取正常的 MariaDB 详细信息。

主要是在进行 test/application.properties

我想运行连接 MariaDB 的应用程序,并且我想在 H2 中运行我的测试用例。请帮忙!

项目结构如下: 项目结构

我的 main/resources/application.properties :

spring.datasource.url=jdbc:mariadb://MYIP/DBNAME
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
server.port=8014
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.Dialect=org.hibernate.dialect.MariaDBDialect

我的 test/resources/application.properties :

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=create-drop
spring.h2.console.enabled=true
logging.level.com.example.demo=DEBUG
server.port=8090

如果我正在运行主应用程序,它仅在 8090 端口上运行并获取 h2 数据库详细信息(如在 test/app.prop 上配置的那样)。我想在主应用程序运行时应该使用 main/resources/app.prop

我的 pom.xml :

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>1.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
             <scope>test</scope>
            <version>1.4.194</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.9.Final</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

标签: javamysqlspringspring-bootmariadb

解决方案


如果您在 gradle 项目中,您必须将 h2 作为测试实现包括在内,还要放上您的项目结构和 application.properties 的屏幕截图,以便我们为您提供帮助

testImplementation "com.h2database:h2:$h2Version"

推荐阅读