micronaut-projects / micronaut-test

Repository for Test Related Utilities for Micronaut
Apache License 2.0
81 stars 60 forks source link

Kotest beforeTest does not work as expected in a MicronautTest #485

Open hantsy opened 2 years ago

hantsy commented 2 years ago

Expected Behavior

In a kotest test ,when setting up beforeTest hooks, it does not work as expected.

The JdbcOperations can not get the connection in the beforetest, but all tests methods are working well.

Actual Behaviour

I want to do some clean database work in the beforeTest.

@MicronautTest(environments = [Environment.TEST], startApplication = false)
class PostRepositoryTest(private val posts: PostRepository, private val template: JdbcOperations) : StringSpec({
   "a test" {//test includes database operations worked well.
    }
}){
    override fun beforeEach(testCase: TestCase) {
       val delCount = this.template.prepareStatement("delete from posts") { it ->
            it.executeUpdate()
       }
       print("deleting items: $delCount")
  }

}

There is an exception thrown by this.template.prepareStatement.

Steps To Reproduce

I used docker compose to bootstrap the database for testing and running applications.

The source codes: https://github.com/hantsy/micronaut-sandbox/blob/criteria-api/jdbc-kotlin/src/test/kotlin/com/example/PostRepositoryTest.kt (the beforeTest is commented out here.)

Environment Information

Example Application

https://github.com/hantsy/micronaut-sandbox/blob/criteria-api/jdbc-kotlin/src/test/kotlin/com/example/PostRepositoryTest.kt

Version

3.2.0

hantsy commented 2 years ago

Dig into the code execution, it requires a transaction to get connection in the beforeTest hook, not sure the reason.

I have updated my example and make it work well, check here.