使用 r2dbc 设置集成测试
Setting up an integration test with r2dbc
import com.expediagroup.api.database.OrderDomain
import com.expediagroup.api.database.OrderDomainRepository
import io.r2dbc.pool.ConnectionPool
import io.r2dbc.spi.ConnectionFactory
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import java.io.IOException
import java.sql.Date
import java.util.Arrays
import java.util.function.Consumer
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.test.context.junit.jupiter.SpringExtension
import reactor.test.StepVerifier
import reactor.core.publisher.Hooks
import org.junit.jupiter.api.BeforeEach
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest
@ExtendWith(SpringExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@DataR2dbcTest
class R2dbcTemplateIT {
@Autowired
var orderDomain: OrderDomainRepository? = null
@Autowired
var database: DatabaseClient? = null
@ClassRule
var mysql: MySQLContainer<?> = MySQLContainer<>("mysql:5.5")
.withDatabaseName("test")
.withUsername("test")
.withPassword("test")
@BeforeEach
fun setUp() {
Hooks.onOperatorDebug()
mysql.start()
val statements: List<String> = Arrays.asList( //
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);"
)
statements.forEach(Consumer { it: String? ->
database!!.sql(it!!) //
.fetch() //
.rowsUpdated() //
.`as`(StepVerifier::create)
.expectNextCount(1) //
.verifyComplete()
})
}
@Test
@Throws(IOException::class)
fun generatesIdOnInsert() {
val domainMetadata = customer(1L, "John", "Smith")
orderDomain?.save(domainMetadata) //
?.`as`(StepVerifier::create) //
?.assertNext { actual ->
assertThat(domainMetadata.id).isNull() // immutable before save
assertThat(actual.id).isNotNull() // after save
}?.verifyComplete()
}
}
我正在尝试 运行 使用 R2DBCRepositories 对 R2DBC 进行集成测试以测试我们的一些东西。我想知道我有这个除了没有任何本地数据库 运行ning 因此
有谁知道关于在此测试中设置数据库的建议吗?
事实证明这是 Kotlin 不喜欢 Java 中的设置方式的问题。
https://github.com/testcontainers/testcontainers-java/issues/318
import com.expediagroup.api.database.OrderDomain
import com.expediagroup.api.database.OrderDomainRepository
import io.r2dbc.pool.ConnectionPool
import io.r2dbc.spi.ConnectionFactory
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import java.io.IOException
import java.sql.Date
import java.util.Arrays
import java.util.function.Consumer
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.test.context.junit.jupiter.SpringExtension
import reactor.test.StepVerifier
import reactor.core.publisher.Hooks
import org.junit.jupiter.api.BeforeEach
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest
@ExtendWith(SpringExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@DataR2dbcTest
class R2dbcTemplateIT {
@Autowired
var orderDomain: OrderDomainRepository? = null
@Autowired
var database: DatabaseClient? = null
@ClassRule
var mysql: MySQLContainer<?> = MySQLContainer<>("mysql:5.5")
.withDatabaseName("test")
.withUsername("test")
.withPassword("test")
@BeforeEach
fun setUp() {
Hooks.onOperatorDebug()
mysql.start()
val statements: List<String> = Arrays.asList( //
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);"
)
statements.forEach(Consumer { it: String? ->
database!!.sql(it!!) //
.fetch() //
.rowsUpdated() //
.`as`(StepVerifier::create)
.expectNextCount(1) //
.verifyComplete()
})
}
@Test
@Throws(IOException::class)
fun generatesIdOnInsert() {
val domainMetadata = customer(1L, "John", "Smith")
orderDomain?.save(domainMetadata) //
?.`as`(StepVerifier::create) //
?.assertNext { actual ->
assertThat(domainMetadata.id).isNull() // immutable before save
assertThat(actual.id).isNotNull() // after save
}?.verifyComplete()
}
}
我正在尝试 运行 使用 R2DBCRepositories 对 R2DBC 进行集成测试以测试我们的一些东西。我想知道我有这个除了没有任何本地数据库 运行ning 因此
有谁知道关于在此测试中设置数据库的建议吗?
事实证明这是 Kotlin 不喜欢 Java 中的设置方式的问题。
https://github.com/testcontainers/testcontainers-java/issues/318