For me, as a software engineer with experience in Java, Kotlin was definitely the language of interest. It is also JVM language, but promised to be improved, concise, readable and beautiful.

So, what are the advantages of Kotlin?

  1. Null safety - null references are detected at compile time.

  2. Fluency - fluent code is readable and easier (and cheaper) to maintain.

  3. Coroutines - a great way to implement non-blocking calls and gracefully handle exceptions.

  4. Multiplatform - Kotlin can be used to write native macOS, iOS, and Windows applications; JavaScript and Android applications.

  5. Interoperability - we can reuse existing Java code by intermixing Java and Kotlin.

In this article I will take a short look on Kotlin world on example of simple Spring Boot RESTfull application. Using Spring Boot with Kotlin makes an application even more elegant. Combination of those two gives us “double expressiveness”.

This simple CRUD KotlinTutor application will implement next API:

POST /kotlinquestions/ - Add new Kotlin question.

GET /kotlinquestions/ - Retrieves all Kotlin questions.

To generate skeleton of Kotlin Spring Boot application Spring initializer can be used. After generation, we got an entry point and can run an application.

class KotlinTutorApplication

fun main(args: Array<String>) {

Kotlin code a bit simpler than Java. No semicolon. The main() function in the Kotlin version is a top-level function, instead of being a member of the class.


For testing purposes I will be using Kotest and Mockk. Kotlin code can be tested also with jUnit. Despite my great love to jUnit I wanted to try new testing tools and follow Kotlin natural style.

After adding necessary dependencies, we can start with the first test.


Kotest has 10 different styles of test layout. I will go for original Kotlin ShouldSpec because I am used to add “should” to test names. To activate Spring extension we need to override extensions() function. it is analog to jUnit’s @ExtendWith.

class KotlinTutorApplicationTest : FreeSpec() {
    override fun extensions() = listOf(SpringExtension)
    init {
        "Should load context" {


Next, we will move to creating controller. We will start with WebMvcTest to check that endpoints work correctly.

class QuestionControllerTest(var mockMvc: MockMvc) : ShouldSpec() {

    override fun extensions() = listOf(SpringExtension)

    @MockkBean(relaxed = true)
    private lateinit var questionsService: KotlinQuestionService

    init {
        should("return all questions") {
            val question1 = kotlinQuestion()
            val question2 = KotlinQuestion(1, "question2", "answer2", 3)
            every { questionsService.getAllQuestions() } returns listOf(question1, question2)

        should("post question") {
            val question = kotlinQuestion()
            val objectMapper = ObjectMapper()
            val json = objectMapper.writeValueAsString(question)
            verify { questionsService.addQuestion(question)

    private fun kotlinQuestion() = KotlinQuestion(0, "question1", "answer1", 4)


We can see that code looks more concise than Java:

  • Property KotlinQuestionService of a class QuestionController can be listed in its declaration.
  • getQuestions() function body can an expression. Its return type is inferred.
class QuestionController(val service: KotlinQuestionService) {

    fun getQuestions() = ResponseEntity.ok(service.getAllQuestions())

    fun addQuestion(@RequestBody kotlinQuestion: KotlinQuestion): ResponseEntity<String> {
        return ResponseEntity.ok("Question is created")


Kotlin generates getters automatically, so we do need to write them. And, since we marked KotlinQuestion as a data class, we get methods equals(), hashCode(), and toString() also for free. As a result we got entity class without any noise.

data class KotlinQuestion(
    @Id @GeneratedValue val id: Long,
    val question: String,
    val answer: String,
    val questionType: Int

Service layer

Service layer might not be necessary for such simple application, but we will create 2 services to see how Service is created with Kotlin and try out more tests with Mockk.

class KotlinQuestionServiceTest : ShouldSpec() {

    @MockK(relaxed = true)
    private lateinit var questionRepository: QuestionRepository

    @MockK(relaxed = true)
    private lateinit var verificationService: VerificationService

    lateinit var kotlinQuestionService: KotlinQuestionService

    override fun beforeTest(testCase: TestCase) {

    override fun afterTest(testCase: TestCase, result: TestResult) {

    init {
        should("save question when verified") {
            val kotlinQuestion = createQuestion()
            every { verificationService.verifyQuestion(kotlinQuestion) } returns true
            every { questionRepository.save(kotlinQuestion) } returns kotlinQuestion
            verify { questionRepository.save(kotlinQuestion) }

        should("throw an exception when not verified") {
            val kotlinQuestion = createQuestion()
            every { verificationService.verifyQuestion(kotlinQuestion) } returns false
            verify(exactly = 0) { questionRepository.save(kotlinQuestion) }

    private fun createQuestion(): KotlinQuestion {
        return KotlinQuestion(1, "Question1", "Answer1", 2)
class KotlinQuestionService(val questionRepository: QuestionRepository, val verificationService: VerificationService) {

    fun getAllQuestions(): Iterable<KotlinQuestion> = questionRepository.findAll()

    fun addQuestion(question: KotlinQuestion) {
        val verified = verificationService.verifyQuestion(question)
        if (verified) {
        } else {
            throw InvalidPropertiesFormatException("Question is not valid")

class VerificationServiceTest : ShouldSpec() {

    init {
        should("return true when type in a range 1 to 5") {
            val verResult = VerificationService().verifyQuestion(createQuestion(4))
            verResult shouldBe true

        should("return false when 0") {
            val verResult = VerificationService().verifyQuestion(createQuestion(0))
            verResult shouldBe false

    private fun createQuestion(type: Int): KotlinQuestion {
        return KotlinQuestion(1, "Question1", "Answer1", type)


When statement makes conditional code more elegant. Especially for processing values of different types.

class VerificationService {

    fun verifyQuestion(kotlinQuestion: KotlinQuestion) =
        when (kotlinQuestion.questionType) {
            in 1..5 -> true
            10 -> true
            else -> false

Spring and Kotlin fit very well together. Spring developers support Kotlin as a first class citizen. Hence, we can create the same application with less code.

Complete implementation with test cases can be found on GitHub.


  1. Programming Kotlin by Venkat Subramaniam.
  2. Kotlin Docs.
  3. Kotest Docs
  4. Mockk Docs