提问者:小点点

带有Spring boot starter数据jpa和Spring boot startor测试的Spring Boot应用程序使Spring找不到应用程序属性


我创建了一个带有Spring boot的小应用程序,它使用Spring数据访问数据库(mySql):

  • Spring引导启动器父级:2.4.2
  • Spring引导起动机网:2.4.2
  • spring boot starter数据jpa:2.4.2
  • mysql连接器java:8.0.22
  • Spring启动装置测试:2.4.2
  • 组织junit。木星:5.7.0
  • junit jupiter引擎:5.7.0

我已经在src/main/Resources中配置了我的application.properties,并配置了url、用户名、密码、驱动程序等属性,如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

我有一个用于访问数据库的存储库接口:

@Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {

}

此外,我已经定义了我的实体。

问题在于当我启动我的webapp时,我得到一个错误,因为它没有从应用程序中读取用于配置数据源的参数。属性:

描述:

未能配置数据源:未指定“url”属性,无法配置嵌入式数据源。

原因:无法确定合适的驱动程序类别

操作:

请考虑以下事项:如果您想要嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径上。如果要从特定概要文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态)。

如果我删除Spring启动启动器测试和 junit 的依赖项,应用程序加载正常,并且可以访问数据库。但是,如果我添加用于添加 junit 的依赖项,它不会读取我的 application.properties 文件,并且我会收到上一个错误。

如何使用junit配置我的应用程序并使用application.properties文件读取应用程序的配置?

先谢谢你!


共2个答案

匿名用户

您的测试/应用程序属性也需要配置。尝试将其添加到测试应用程序属性文件中

spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

如果你还不支持h2,这是你的pom。

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

匿名用户

检查您的应用程序。属性

改变

spring.datasource.driver=com.mysql.jdbc.Driver

spring.datasource.driver-class-name=com.mysql.jdbc.Driver