【问题标题】:Test application.properties loading during executing Spring Boot App, instead of main/application.prop在执行 Spring Boot App 期间测试 application.properties 加载,而不是 main/application.prop
【发布时间】:2020-11-04 19:00:24
【问题描述】:

我已经通过 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 详细信息。

主要是测试/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

我的测试/资源/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>

【问题讨论】:

  • 尝试添加:''' src/main/resourcestrue ''' 然后机器人也在工作。
  • @pvpkiran 你能调查一下吗?

标签: java mysql spring spring-boot mariadb


【解决方案1】:

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

testImplementation "com.h2database:h2:$h2Version"

【讨论】:

  • 嗨,我正在运行 Maven 构建。附上项目结构截图
  • 然后将范围添加到您的依赖项```` com.h2databaseh2${h2.version} 测试 ````
  • 我已经添加了依赖项--> 我的 h2 正在运行。我的问题是:一切都在 H2 上运行(测试和主应用程序)。我想在 H2 数据库和 MySQL 上的主应用程序上运行测试。
猜你喜欢
  • 2020-09-07
  • 2016-03-04
  • 2018-07-16
  • 1970-01-01
  • 2019-08-23
  • 2014-11-30
  • 2015-01-03
  • 2020-10-23
  • 1970-01-01
相关资源
最近更新 更多