1

I am new to Kotlin-Exposed Framework. What I am looking for is to have a Global DB schema in H2 Database - jdbc:h2:mem:test for my test cases.

In my application, I am using mysql. I want to use H2 only for testing.

Below is my code snippet for reference.


object ConnectToDatabase : TestListener {
    var database: Database? = null
    override suspend fun beforeSpec(spec: Spec) {
        database = Database.connect("jdbc:h2:mem:testdb", driver = "org.h2.Driver")
    }
}


class StudentProcessorTests : FreeSpec() {
    override fun listeners(): List<TestListener> = listOf(ConnectToDatabase)
    val DaggerObj = DaggerObject.create()
    val studentProcessor = DaggerObj.studentProcessor();



    private fun createTable(table: Table) {
        transaction {
            SchemaUtils.create(table)
        }
    }


    private fun insertSampleRowForStudent() {
        transaction {
            Student.insert {
                //code
            }
        }
    }

    private fun insertSampleRowForDepartment() {
        transaction {
            Department.insert {
                //code
            }
        }
    }


    init {

            "Case1"   {
                transaction {
                    createTable(Student)
                    createTable(Department)
                    createTable(Faculty)
                    insertSampleRowForStudent()
                    insertSampleRowForDepartment()
                    val response = studentProcessor.createStudent(StudentObject)
                    Assert.assertTrue(response > 0)
                }
            }

            "Case2" {
                transaction {
                    createTable(Student)
                    createTable(Department)
                    createTable(Faculty)
                    insertSampleRowForStudent()
                    insertSampleRowForDepartment()
                }
            }

        }

}

Here, Everytime I have to write - createTable for each test cases. Is there a way by which I can define this Schema globally and can get rid of this part for every test cases.

RKP
  • 750
  • 2
  • 12
  • 23
  • Take a look if project wide config seems to cut the deal for you https://github.com/kotest/kotest/blob/master/doc/project_config.md – LeoColman May 05 '20 at 14:39

0 Answers0