Spring 启动应用程序不使用 application-test.properties 中定义的 DB 驱动程序 代码

问题描述

我决定将一个 h2 DB 添加到我的 Spring Boot 应用程序中以进行测试。我在我的文件application-test.properties添加一个新的 test/resources,其中包含所有必要的配置。问题是当我运行测试时,我总是能连接到 MysqL 数据库

我可以看到正在使用 application-test.properties,因为在我的测试中 loggin.levelDEBUG 更改为 ERROR

对于我的 MysqL 数据库我有一个提供数据源的文件

代码

application-test.properties 文件

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username = sa
spring.datasource.password =
spring.datasource.driver-class-name = org.h2.Driver

spring.h2.console.enabled=false

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.springframework.security=ERROR

application.properties 文件

MysqL.database.host=localhost
MysqL.database.port=3306
MysqL.database.db=mydb
MysqL.database.user=root
MysqL.database.password=root
MysqL.database.driver=MysqL

logging.level.org.springframework.security=DEBUG

datasource

@Bean
open fun getDataSource(): DataSource? {
    val dataSourceBuilder = DataSourceBuilder.create()
    dataSourceBuilder.url("jdbc:$driver://$databaseHost:$databasePort/$databaseName")
    dataSourceBuilder.username(databaseUser)
    dataSourceBuilder.password(databasePassword)
    return dataSourceBuilder.build()
}

test

@SpringBoottest
@AutoConfiguremockmvc
@ActiveProfiles("test")
class AdAccountControllerTest {

    @Autowired
    lateinit var mockmvc: mockmvc

    @Test
    fun `we should get unauthorized status when we dont provide credentials`() {
        mockmvc.perform(get("/api/v1/ad-account"))
                .andExpect(status().isOk)
                .andDo(print())
    }


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)