【问题标题】:Setting up an integration test with r2dbc使用 r2dbc 设置集成测试
【发布时间】:2021-07-18 01:22:33
【问题描述】:

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 上运行集成测试来测试我们的一些东西。我想知道我有这个,除了没有运行任何本地数据库的事实

有没有人知道在这个测试中设置数据库的建议?

【问题讨论】:

    标签: spring integration testcontainers r2dbc


    【解决方案1】:

    事实证明,Kotlin 不喜欢它在 Java 中的设置方式是一个问题。

    https://github.com/testcontainers/testcontainers-java/issues/318

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多