Hey! I'm trying to test an application with an in ...
# exposed
n
Hey! I'm trying to test an application with an in memory sqlite database. My test class looks like this:
Copy code
class ProjectRepositoryTest {
    companion object {
        val database = Database.connect("jdbc:sqlite:file:test?mode=memory&cache=shared", "org.sqlite.JDBC")
        val projectRepository = ProjectRepository(database)
        const val initialNumberOfProjects = 10

        @BeforeAll
        @JvmStatic
        fun setUp() {
            database.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
            database.transactionManager.defaultRepetitionAttempts = 1
        }
    }

    @BeforeEach
    fun setUpEach() {
        transaction(database) {
            addLogger(StdOutSqlLogger)
            SchemaUtils.create(ProjectsTable)
        }
    }

    @Test
    fun testGetAllProjects() {
        println("Creating initial projects.")
        transaction(database) {
            addLogger(StdOutSqlLogger)
            repeat(initialNumberOfProjects) { i ->
                println("\tCreated project $i.")
                ProjectEntity.new {
                    name = "Project $i"
                    description = "Description $i"
                    archived = 0b0
                }
            }
        }
        ...
    }

    @AfterEach
    fun tearDown() {
        transaction(database) {
            SchemaUtils.drop(ProjectsTable)
        }
    }
}
I'm getting a database does not exist exception, saying that the Projects table doesn't exist. What am I doing wrong?
l
Try to create table inside
@BeforeAll
block
n
I tried that now. it still throws the same error.
@Leonid Yavorskyi Could it be because of the execution order of the transactions?
s
I suspect its cleared once the number of connections to the db reaches 0 which seems to be right after
setUpEach()
.
n
Found the solution. For some reason if you connect to sqlite with this connection string
jdbc:sqlite:file:test?mode=memory&cache=shared
, then it does not work, and throws the error I said above. If you use
jdbc:sqlite:test:?mode=memory&cache=shared
though, then it works no problem.
👌 1