我编写了一个简单的存储库来测试 Kotlin Exposed with TestContainers。我使用的数据库是mysql。
这是我的代码:
class StudentsRepositoryTest: ShouldSpec({
val container = getMysqlContainer()
val mysqlJdbcUrl = container.jdbcUrl
beforeSpec {
Database.connect(mysqlJdbcUrl, "com.mysql.cj.jdbc.Driver")
transaction {
SchemaUtils.create(Students)
}
}
... // some tests
private fun getMysqlContainer(): MySQLContainer<Nothing> {
return MySQLContainer<Nothing>("mysql:5.7").apply {
withUsername("root")
withPassword("")
withEnv("MYSQL_ROOT_PASSWORD", "%")
withDatabaseName("test")
start()
}
}
代码在 beforeSpec
处失败Access denied for user ''@'172.17.0.1' (using password: NO)
也许我错过了一些东西,任何帮助将不胜感激我使用的库:
最佳答案
我终于找到了我的问题的答案。原因是我忘记将用户名和密码传递给与 Exposed 库的连接。
val mysqlJdbcUrl = container.jdbcUrl
val username = container.username
val password = container.password
Database.connect(mysqlJdbcUrl, "com.mysql.cj.jdbc.Driver", username, password)
关于kotlin - TestContainers 和 Mysql : Access denied for user '' @'172.17.0.1' (using password: NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63957420/