Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

kotlin - Quarkus: REST resource can't find entities created in test

I'm trying to create a test for a REST resource I've created with Quarkus, Panache & Kotlin. The test first writes a few entities to the database and then performs a REST call that should return these entities.

However the REST resource can't seem to find the entities. If I query the database directly in the test, the entities are there.

My code looks like this:

@Entity
open class TestEntity : PanacheEntity() {

    companion object : PanacheCompanion<TestEntity, Long> {

        fun createTestString(): String {
            return "Found ${TestEntity.count()} test entities: ${
                TestEntity.listAll().joinToString(", ") { it.value }
            }"
        }
    }

    @Column(nullable = false)
    lateinit var value: String

}
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
@Transactional
class TestResource {

    @GET
    fun test() = TestEntity.createTestString()

}
@QuarkusTest
@QuarkusTestResource(H2DatabaseTestResource::class)
@TestTransaction
class TestResourceTest {

    @Test
    fun print() {
        val entity1 = TestEntity()
        entity1.value = "foo"
        entity1.persist()

        val entity2 = TestEntity()
        entity2.value = "bar"
        entity2.persist()

        println(
            "TEST: ${TestEntity.createTestString()}"
        )

        RestAssured.given()
            .`when`().get("/test")
            .then()
            .statusCode(200)
            .body(CoreMatchers.startsWith("Found 2 test entities"))
    }
}

The println() in the test prints TEST: Found 2 test entities: foo, bar. But the REST call returns Found 0 test entities:.

Does anyone have any idea what I'm doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...