https://kotlinlang.org logo
#exposed
Title
# exposed
v

Vinicius Araujo

06/13/2019, 4:52 PM
Does anyone have an example on how to use a H2 connection for unit testing, and use the default connection for real usage without setting the connection by parameter?
j

Jonathan Mew

06/14/2019, 7:46 AM
What unit testing framework are you using? If you just have a single database you can set up the connection in an @BeforeAll rule and not supply the database to your transactions. Or perhaps I haven't quite understood the question...
v

Vinicius Araujo

06/14/2019, 2:49 PM
You got it right. I'm using KotlinTest and it has this feature. I'll try it.
The default (postgres) transaction is automatically created inside the service method, and I want my test to call this this method, but using the H2 connection instead. Not sure how to do this
j

Jonathan Mew

06/14/2019, 3:01 PM
Do you ever need to communicate with the postgres database in your unit tests?
It's perhaps helpful to draw a distinction between connecting to a database (building a
Database
object) and creating a transaction. The code creating the transaction doesn't have to supply a database if you only talk to a single database during your tests - just avoid connecting to the other database during testing. If you need to connect to both during testing, the code that creates a transaction will need access to a
Database
object created by `connect`ing to the correct db.
v

Vinicius Araujo

06/14/2019, 3:54 PM
I do not need to connect postgres from test. This is how I connect the DB:
Copy code
object DatabaseFactory {
    fun hikari(): HikariDataSource {
        val config = HikariConfig().also {
            it.username = sqlConfig.username
            it.password = sqlConfig.password
            it.jdbcUrl = sqlConfig.url
        }

        return HikariDataSource(config)
    }
}


val connectSql by lazy {
    Database.connect(DatabaseFactory.hikari())
}

suspend fun <T> dbQuery(block: () -> T): T =
    withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        try {
            connectSql
        }catch (e: Exception) {
            println(e.stackTrace)
            throw ErroInternoException(codigo = 501, msgInterna = "Erro de conexao com banco de dados")
        }
        transaction { block() }
    }
So at the service method I only use:
Copy code
dbQuery { query inside }
I wish I could set the H2 DB from test without changing the default factory
j

Jonathan Mew

06/14/2019, 4:06 PM
Sorry, I haven't got any experience with Hikari (nor have I actually used H2...). But I think you can do your connection any time before your first query, so perhaps you can separate it in a way that is reliably called by your application on startup, but isn't so close to your db access.
then you can do an H2
Database.connect()
in that @BeforeAll
and avoid calling into your DatabaseFactory in your tests
v

Vinicius Araujo

06/14/2019, 7:33 PM
I got this! KotlinTest let me insert environment variables for the test runtime, so I put connection details that are overrided within the test, also created the db tables in the BeforeSpec you mentioned. Now working like a charm 🙂 Thanks for your help
j

Jonathan Mew

06/15/2019, 8:52 PM
🎉
5 Views