# Test Kotlin projects with Maven Kotlin integrates seamlessly with the Maven ecosystem, allowing you to use industry-standard tools to verify your backend applications. In this guide, you'll learn how to create tests with JUnit and use Maven plugins to run unit and integration tests. Tip: For a detailed guide to setting up your Maven project to use both Kotlin and Java, see [Project configuration](mixing-java-kotlin-intellij.html#project-configuration). ## Create tests with JUnit [JUnit](https://junit.org/) is the standard testing framework for Kotlin backend development. While Kotlin supports multiple JUnit versions, most modern projects should use JUnit 6. To create a test in Kotlin using JUnit, use the `@Test` annotation from the `kotlin.test` or JUnit package. ### Add dependency The `kotlin-test` library is the easiest way to start. It provides a common set of assertions and automatically pulls in the necessary JUnit artifacts. #### JUnit 5 and later For all new projects, use the `kotlin-test-junit5` artifact. It provides full support for JUnit, including features like nested tests and parallel execution. Kotlin/JVM supports the latest stable JUnit version, JUnit 6. Update your `pom.xml` file as follows: ```XML org.jetbrains.kotlin kotlin-test-junit5 2.4.21 test ``` Note: Despite its name, `kotlin-test-junit5` supports all latest JUnit versions, including JUnit 6. #### JUnit 4 If you'd like to use an earlier version of JUnit, for example, for a legacy project, use the `kotlin-test-junit` artifact that utilizes JUnit 4: ```XML org.jetbrains.kotlin kotlin-test-junit 2.4.21 test ``` Tip: For a detailed guide on using JUnit for testing and a sample project, see the [Test Java code with Kotlin](jvm-test-using-junit.html) tutorial. ### Write unit tests Unit tests verify isolated parts of your code, such as individual functions or classes. By convention, unit tests are named with the `*Test` suffix. For example: ```KOTLIN import kotlin.test.Test import kotlin.test.assertEquals class OrderServiceTest { @Test fun `calculate total should sum item prices`() { val service = OrderService() val result = service.calculateTotal(listOf(10.0, 25.0)) assertEquals(35.0, result) } } ``` ### Write integration tests Integration tests verify interaction between components, like a service and a database. By convention, integration tests are named with the `*IT` suffix. For example: ```KOTLIN import kotlin.test.Test import kotlin.test.assertNotNull class UserRepositoryIT { @Test fun saveFindUser() { // Example integration with a database or service val repository = UserRepository() repository.save(User("KotlinUser")) val user = repository.findByName("KotlinUser") assertNotNull(user) } } ``` ## Run tests In Maven projects, test execution is typically split between two plugins: Surefire and Failsafe to ensure a clean build lifecycle. ### With Surefire plugin The [Surefire plugin](https://maven.apache.org/surefire/maven-surefire-plugin/) handles unit tests. It runs all Kotlin and Java tests that follow the `*Test` naming pattern. By default, it executes during the `test` phase of the build lifecycle and fails the build immediately if a test fails. ```XML org.apache.maven.plugins maven-surefire-plugin 3.5.5 ``` To run only your unit tests, use the following command: ```BASH mvn test ``` ### With Failsafe plugin The [Failsafe plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/) handles integration tests. It runs all Kotlin and Java tests that follow the `*IT` naming pattern. Unlike Surefire, Failsafe allows the build to continue if a test fails during the `integration-test` phase, allowing the `post-integration-test` phase tasks (like stopping a Docker container) to run. The build finally fails during the `verify` phase if there were any test failures. ```XML org.apache.maven.plugins maven-failsafe-plugin 3.5.5 integration-test verify ``` To run both unit and integration tests, use the following command: ```BASH mvn verify ``` ## Get detailed failure messages The Kotlin [Power-assert compiler plugin](power-assert.html) generates detailed failure messages that display intermediate values in your assertions, giving you the full diagrams in the console output. 1. To enable Power-assert, add the following configuration to your project's `pom.xml`: ```XML 2.4.21 17 org.jetbrains.kotlin kotlin-test-junit5 ${kotlin.version} test org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} true compile process-sources compile test-compile process-test-sources test-compile power-assert org.jetbrains.kotlin kotlin-maven-power-assert ${kotlin.version} ``` * In the `` section, set the Kotlin version and the target JDK release version, for example, JDK 17. * In the `` section, add `kotlin-test-junit5` for running tests. * In the `` section, configure `kotlin-maven-plugin` with the `power-assert` compiler plugin. List the assertion functions you want to transform under `` and add the Power-assert plugin dependency. 2. Create a test file, `UserProfileBackendTest.kt`, in the `src/test/kotlin` directory: ```KOTLIN import kotlin.test.Test import kotlin.test.assertEquals class UserProfileBackendTest { data class UserProfile(val id: Long, val email: String) @Test fun `verify backend entity mapping matches`() { // Define expected object state val expectedRecord = UserProfile(id = 451L, email = "admin-dev@company.internal") // Simulate a mismatched backend response val actualRecord = UserProfile(id = 451L, email = "admin-prod@company.com") // Use a standard kotlin.test assertion assertEquals(expectedRecord, actualRecord, "Profile configurations out of sync") } } ``` 3. Run the test and verify the output: ```BASH mvn test ``` The console displays the full Power-assert diagram, showing the difference between the expected and actual values at each position in the expression: ```TEXT Profile configurations out of sync assertEquals(expectedRecord, actualRecord, "Profile configurations out of sync") | | | UserProfile(id=451, email=admin-prod@company.com) UserProfile(id=451, email=admin-dev@company.internal) ``` ## Explore other testing frameworks Besides JUnit, you can use other popular frameworks to make Kotlin tests more idiomatic and readable: | Library |Description | ------------------------ | [AssertJ](https://github.com/assertj/assertj) |Fluent assertion library with chainable assertions. | | [Mockito-Kotlin](https://github.com/mockito/mockito-kotlin) |Kotlin wrapper for Mockito that provides helper functions and better integration with the Kotlin type system. | | [MockK](https://github.com/mockk/mockk) |Native Kotlin mocking library that supports Kotlin-specific features, including coroutines and extension functions. | | [Kotest](https://github.com/kotest/kotest) |Assertion library for Kotlin offering multiple assertion styles and extensive matcher support. | | [Strikt](https://github.com/robfletcher/strikt) |Assertion library for Kotlin with type-safe assertions and support for data classes. | ## What's next * Explore the features of the [kotlin.test library](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/). * Learn more about the [Power-assert compiler plugin](power-assert.html).