# How to use an in-memory database for testing on Ktor

DevFeed: [How to use an in-memory database for testing on Ktor](<https://devfeed.tech/articles/how-to-use-an-in-memory-database-for-testing-on-ktor-25560.md>)

Original publisher: [Read original article](<https://www.marcogomiero.com/posts/2021/ktor-in-memory-db-testing/>)

Author: Marco Gomiero

Published: 2021-10-04T00:00:00Z

Content type: tutorial

Language: en

Sources: [Posts on Marco Gomiero](<https://devfeed.tech/sources/posts-on-marco-gomiero.md>)

Topics: [in-memory database](<https://devfeed.tech/topics/in-memory-database.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Database](<https://devfeed.tech/topics/database.md>), [Ktor](<https://devfeed.tech/topics/ktor.md>), [Back end](<https://devfeed.tech/topics/backend.md>)

Tags: [database](<https://devfeed.tech/tags/database.md>), [exposed](<https://devfeed.tech/tags/exposed.md>), [h2](<https://devfeed.tech/tags/h2.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [in-memory-database](<https://devfeed.tech/tags/in-memory-database.md>), [ktor](<https://devfeed.tech/tags/ktor.md>), [mysql](<https://devfeed.tech/tags/mysql.md>), [orm](<https://devfeed.tech/tags/orm.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

Automated tests need a database they can reset or prepopulate to cover both empty and populated states. The article chooses an in-memory H2 database for tests in a Ktor project that uses MySQL in production; because its data is held in memory, closing the connection clears it. It contrasts this with starting a dedicated Docker database container for each run, which can more closely match production, and notes a critique of in-memory testing databases. The project uses JetBrains Exposed, which supports H2 and several other databases. The supplied excerpt does not include the concrete dependency or configuration steps.

## Source excerpt

SERIES: Building a backend with Ktor Part 1: Structuring a Ktor project Part 2: How to persist Ktor logs Part 3: How to use an in-memory database for testing on Ktor Part 4: How to handle database migrations with Liquibase on Ktor Part 5 Generate API documentation from Swagger on Ktor Part 6: How to schedule jobs with Quartz on Ktor Part 7: Moving from mobile to backend development with Ktor Usually, in a backend project, there are different instances of the same database: one for production (or more than one, it depends on the architecture), one for staging, and a local one that runs in the development machine.