Spring Boot 2.0 supports writing test classes for Kotlin component testing. This recipe will highlight how to test a RESTful service written in the Kotlin language.
Using Kotlin language, perform the following steps in implementing test cases for Kotlin request handlers:
- First, add the Kotlin-JUnit Maven dependency into the
pom.xml
file:
<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test-junit</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency>
- Add a JUnit 4 plugin.
- Inside
src/test/kotlin
, create anorg.packt.microservice.kotlin.test
package and add inside it a Kotlin test class that will test one of the Kotlin REST endpoints:
import org.junit.Assert.*; @RunWith(SpringRunner::class) @SpringBootTest(webEnvironment = ...