Search⌘ K
AI Features

Testing the Service and Modularizing the Application

Understand how to write and run tests in Kotlin for Ktor microservices by modularizing your application. Learn to configure server modules to enable effective testing and improve code organization.

Writing a test in the AppTest.kt file

To write our first test, let’s create a new file called AppTest.kt under the src/test/kotlin directory. Now, let’s add a new dependency:

dependencies {
     ...
     testImplementation("io.ktor:ktor-servertests:$ktorVersion")
}

See the build.gradle.kts file for step 5 in the code. Next, let’s add the following contents to our AppTest.kt file:

internal class ServerTest {
     @Test
     fun testStatus() {
         withTestApplication {
             val response = handleRequest(HttpMethod.Get,
                 "/status").response
             assertEquals(HttpStatusCode.OK,
...