Spring Boot 测试未到达 MySql 数据库 奇怪的问题我的代码

问题描述

大家好,

我正在对我用 Kotlin 编写的一个小型 Spring Boot 应用程序进行一些测试。我正在使用 jdbc 为这个项目使用 MysqL 数据库。一切都在 Docker 上运行。

我目前正在测试我的控制器,但我的数据库连接遇到了一些问题。我的测试总是失败,这是我每次收到的消息:Request processing Failed; nested exception is com.MysqL.cj.jdbc.exceptions.CommunicationsException: Communications link failure

奇怪的问题

所以看起来应用程序没有连接到数据库,但实际上它只是在测试范围内没有连接。当我在浏览器中请求相同的 url 时,我没有遇到任何问题。我目前认为存在一些错误配置导致与数据库的测试连接失败。但我不知道问题出在哪里。

此外,如果我对不查询数据库的端点运行测试,它也会通过。例如,数据库正在运行,因为我可以在使用 curl 时获得所需的结果。它仅在我运行测试时失败。

我的代码

我的测试看起来像这样

@SpringBoottest
@AutoConfiguremockmvc
class AdAccountControllerTest {

    @Autowired
    lateinit var mockmvc: mockmvc

    @Test
    fun `we should get the ad-accounts`() {
        mockmvc.perform(get("/api/v1/ad-account"))
                .andExpect(status().isOk)
                .andDo(print())
    }
}

我的 application.properties 如下所示:

MysqL.database.host=database
MysqL.database.port=3306
MysqL.database.db=application
MysqL.database.user=root
MysqL.database.password=root

logging.level.org.springframework.security=DEBUG

我的网络验证器

@Configuration
@EnableWebSecurity
open class WebAuthenticationConfigurator : WebSecurityConfigurerAdapter() {

    @Autowired
    private val dataSource: DataSource? = null

    @Autowired
    @Throws(Exception::class)
    fun configureGlobal(auth: AuthenticationManagerBuilder) {
        auth.jdbcAuthentication()
            .dataSource(dataSource)
            .passwordEncoder(passwordEncoder())
            .usersByUsernameQuery("select email as username,password_hash as password,1 from user where email=?")
            .authoritiesByUsernameQuery("select email as username,'ADMIN' as role from user where email=?")
    }

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
            .antMatchers("/api/v1/**").permitAll()
            .anyRequest().denyAll()
            .and()
            .httpBasic()
            .and().cors().disable()
    }
}

我的数据源

@Configuration
open class AuthenticationDataSource {

    @Value("\${MysqL.database.host}")
    private val databaseHost: String? = null

    @Value("\${MysqL.database.port}")
    private val databasePort: String? = null

    @Value("\${MysqL.database.db}")
    private val databaseName: String? = null

    @Value("\${MysqL.database.user}")
    private val databaseUser: String? = null

    @Value("\${MysqL.database.password}")
    private val databasePassword: String? = null

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

在这里错过了什么?我认为很明显我的测试环境无法与数据库建立连接,我是否必须配置一些额外的东西才能使其与测试一起工作?

感谢您的时间。

解决方法

我发现我的方法有问题。这一切都与 application.properties 有关。我的 mysql.database.host 的值不正确。主机的值是在容器内使用的值。由于测试是从容器范围之外调用的,因此应用程序未到达导出数据库。