使用r2dbc设置集成测试

pvabu6sv  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(482)
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上运行一个集成测试来测试我们的一些东西。我想知道我有这个除了没有任何本地数据库运行的事实

有人知道在这个测试中设置db的建议吗?

8fq7wneg

8fq7wneg1#

原来这是因为kotlin不喜欢java中的设置方式。
https://github.com/testcontainers/testcontainers-java/issues/318

相关问题