我创建了一个具有以下定义的实体。运行测试后,我看到数据库“DB_Name”不存在。我为数据库和模式创建提供了初始脚本,但我仍然遇到问题。问题是代码中有 schema = "dbo", catalog = "DB_Name"。
package com.test.test.entity
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
@Entity
@Table(name = "TestEntity" , schema = "dbo", catalog = "DB_Name")
class TestEntity {
@Id
var id: Long? = null
}
初始脚本:
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DB_Name')
BEGIN
CREATE DATABASE [DB_Name]
END;
IF NOT EXISTS ( SELECT *
FROM sys.schemas
WHERE name = N'dbo' )
Begin
EXEC('Use [DB_Name] ');
EXEC('CREATE SCHEMA [dbo] ');
end
测试类:
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class TestTestEntityRepository {
companion object {
@Container
private val msSQLContainer = MSSQLServerContainer<Nothing>(
DockerImageName.parse("mcr.microsoft.com/mssql/server:2017-latest")
.asCompatibleSubstituteFor("mcr.microsoft.com/mssql/server"))
@DynamicPropertySource
@JvmStatic
fun registerDynamicProperties(registry: DynamicPropertyRegistry) {
msSQLContainer.acceptLicense()
msSQLContainer.start()
msSQLContainer.withInitScript("sql/init-cobra.sql")
registry.add("spring.datasource.url", msSQLContainer::getJdbcUrl)
registry.add("spring.datasource.username", msSQLContainer::getUsername)
registry.add("spring.datasource.password", msSQLContainer::getPassword)
database.close()
}
}
@Autowired
private lateinit var usersRepository: TestRepository
@Test
fun tes() {
Assert.assertNotNull(usersRepository)
}
@Test
fun testInteractionWithDatabase() {
ScriptUtils.runInitScript(JdbcDatabaseDelegate(msSQLContainer, ""), "sql/init-cobra.sql")
val testEntity:TestEntity= TestEntity()
testEntity.id=12
}
}
我该如何解决这个问题?