Kotlin Unit Testing Examples

Overview

Kotlin Unit Testing Examples

This repository contains examples of basic unit tests written in Kotlin. In specific directories you can find gradle buildscript with needed dependencies and configuration, simple unit test and parameterized test.

All sample projects was verified under Gradle 4.10.2 and Intellij IDEA 2018.2.4

Application

UseCase which provides empty string - to showcase structure of test methods

DistanceConverter - converts meters to kilometers in parameterized tests

MVP - Model-View-Presenter contract

Gradle, Kotlin & Groovy

This Gradle project is by default configured with Gradle’s Kotlin DSL

The equivalent Groovy build are still available for you copy&pasting pleasures, they have been renamed as build.gradle.groovy

You can switch between Groovy and Kotlin by running the script $ ./toggle-kotlin-groovy.sh

Junit4

Attach to project

dependencies{
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Junit5

Attach to project

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
    testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

KotlinTest

Attach to project

dependencies{
    testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.10'
}

Spek

Attach to project

repositories {
    mavenCentral()
    maven { url "https://dl.bintray.com/spekframework/spek-dev" }

}

test {
    useJUnitPlatform {
        includeEngines 'spek2'
    }
}

dependencies {
    testImplementation ('org.spekframework.spek2:spek-dsl-jvm:2.0.0-rc.1')  {
        exclude group: 'org.jetbrains.kotlin'
    }
    testRuntimeOnly ('org.spekframework.spek2:spek-runner-junit5:2.0.0-rc.1') {
        exclude group: 'org.junit.platform'
        exclude group: 'org.jetbrains.kotlin'
    }

    testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')


    // spek requires kotlin-reflect, can be omitted if already in the classpath
    testRuntimeOnly "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

For Spek you may also need additional Intellij or Android Studio plugin

Mockito

Add to project

repositories {
    mavenCentral()
    jcenter()
}


dependencies {
    testCompile "org.mockito:mockito-core:2.23.0"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Mockito-Kotlin

Add to project

repositories {
    mavenCentral()
    jcenter()
}


dependencies {
    testCompile "org.mockito:mockito-core:2.23.0"
    testCompile "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Mockk

Add to project

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testImplementation "io.mockk:mockk:1.8.12"
}

Strikt

Add to project

dependencies {
    testImplementation("io.strikt:strikt-core:0.17.0")
}
Comments
  • Convert project to Gradle+Kotlin

    Convert project to Gradle+Kotlin

    While I was doing this small pull request earlier, I realized that your project was exactly the playground I needed (neither trivial not too large, with tests) for something I had in mind :

    Showing by example how to convert a Gradle project to the Kotlin DSL.

    It will be a follow-up to this article I wrote and I plan to show it at a Kotlin Meetup in Berlin.

    https://blog.kotlin-academy.com/gradle-kotlin-the-missing-piece-of-the-puzzle-7528a85f0d2c

    If you prefer to stay with Gradle/Groovy, no hard feeling :)

    It should work fine with Intellij IDEA 2018.+ and Android Studio 3.2+, tell me if you encounter problems.

    opened by jmfayard 5
  • Improve kotlintest parametrized test

    Improve kotlintest parametrized test

    Personally I would write this test like this. Both cleaner to read, and a better test output


    java.lang.AssertionError: 1 elements passed but expected 2
    
    The following elements passed:
    (500, 0.5)
    
    The following elements failed:
    (750, 0.8) => 0.75 should be equal to 0.8
    
    opened by jmfayard 4
  • New KotlinTest module for a Kotlin Academy article

    New KotlinTest module for a Kotlin Academy article

    Hello Jaroslaw,

    I have a plan to write an article about KotlinTest for https://blog.kotlin-academy.com

    I contributed to this project in the past (the asciidoc and Gradle Kotlin DSL parts), and I thought it would be handy to have the code samples I will use to be part of this project. It's a good way to have a unit tests playground already setup, and people can compare with what is available in the other unit tests frameworks.

    I don't want to bother you each time I make a modification to my code samples though.

    Would you mind to give me write access to this repo?

    Jean-Michel Fayard

    opened by jmfayard 1
  • Invert actual and expected

    Invert actual and expected

    When using shouldBe, the syntax should be something similar to "this should be that", and not "that should be this". With this change, the semantics are fixed

    opened by LeoColman 0
  • Use Versions.xx in README.adoc

    Use Versions.xx in README.adoc

    Hello again,

    So first I just wanted to handle the nice comments added by gildor in the previous pull request https://github.com/rozkminiacz/KotlinUnitTesting/pull/2

    Then I said, why not update buildSrc/src/main/kotlin/Versions.kt? Notably Kotlin 1.3.0. The project still works nice!

    Then I realized I had a problem, the README does not auto-update itself if I update Versions.kt

    Then I realized there is a solution for that too. I could use variables in the README if I convert it from Markdown to https://asciidoctor.org/ (which is like Markdown for the simple things, but without the pile of hacks when you want to do something more complex. Because then I can generate the variables for the README from Gradle!

    Ok, so that was more time than I expected but it was fun!

    opened by jmfayard 0
  • Add TestNG to samples

    Add TestNG to samples

    Create new module named testng with samples of TestNG framework usage. testng.org/

    • [ ] Gradle and Intellij configuration
    • [ ] Unit test example
    • [ ] Parameterized test example
    • [ ] Example of test grouping
    enhancement help wanted 
    opened by rozkminiacz 0
Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin Gradle DSL.

SampleCompose Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin

Mohammadali Rezaei 7 Nov 28, 2022
🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!

Kotlin Coroutines - Use Cases on Android ?? Learning Kotlin Coroutines for Android by example. ?? Sample implementations for real-world Android use ca

Lukas Lechner 2.1k Jan 3, 2023
It is a project that contains lessons and examples about Kotlin programming language. 🇰

Kotlin Tutorials What is Kotlin? I added the platforms it supports and great resources. You can access the article from the link below: https://medium

Halil Özel 94 Dec 22, 2022
Kotlin Examples Problems

Kotlin Examples Problems --->>> Repo: Getting Started Kotlin <<<--- --->>> Repo Kotlin Koans <<<--- --->>> Repo: GameBoy Emulator Enviroment <<<--- --

Victor Bolinches 22 Oct 3, 2022
kotlin koans examples

Kotlin Koans Build --->>> Repo: Getting Started Kotlin <<<--- --->>> Repo: Problems Kotlin <<<--- --->>> Repo: GameBoy Emulator Enviroment <<<--- --->

Victor Bolinches 121 Oct 3, 2022
101 examples for Kotlin Programming language.

This is a collection of runnable console applications that highlights the features of Kotlin programming language. The use of console application enab

Dody Gunawinata 192 Dec 1, 2022
Full stack examples of how to use Hotwire JS in Kotlin services

hotwire-kt A collection of Kotlin examples using the Hotwire JS framework to build interactive web apps with a Kotlin Armeria server backend. Using Ho

Andrew (Paradi) Alexander 9 Dec 14, 2022
Getting started Kotlin - Examples and explanations

Getting started Kotlin I'm learning Kotlin, so I have been updating it with examples and explanations about the language that I'm using at work. Proje

Jean Jacques Nascimento Barros 2 Apr 25, 2022
Examples for using Kotlin at a basic level for Android application development.

Kotlin Android Jetpack Basics Ejemplos para usar Kotlin a nivel básico para el desarrollo de aplicaciones Android. Kotlin Android Jetpack Basics Acerc

José Luis González Sánchez 2 Jun 28, 2022
A coding examples project about Kotlin Programming language. 🇰

Kotlin Tutorial ????‍?? What is Kotlin ❓ Kotlin is a new programming language, developed by JetBrains. Jetbrains is a popular software development com

Mustajab Ikram 4 Oct 11, 2022
A Movie listing app with unit tests and a couple of UI tests

MovieListings App This app is a Movie listing app with unit tests and a couple of UI tests. The app gets a list of movies from a public API, and the d

Abayomi Akanji 1 Dec 1, 2021
Examples of Getting Started vídeos

Getting Started Kotlin Learn the basics of getting started with kotlin --->>> Repo: Kotlin Koans <<<--- --->>> Repo: Problems Kotlin <<<--- --->>> Rep

Victor Bolinches 41 Dec 4, 2022
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

Karumi 44 Oct 3, 2022
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Karumi 61 Nov 20, 2022
Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.

KataScreenshot in Kotlin We are here to practice UI testing using screenshot tests for Android. We are going to use Espresso to interact with the Appl

Karumi 76 Nov 20, 2022
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.

KataSuperHeroes in Kotlin We are here to practice UI Testing. We are going to use Espresso to interact with the Application UI. We are going to use Ko

Karumi 86 Nov 20, 2022
Nice and simple DSL for Espresso Compose UI testing in Kotlin

Kakao Compose Nice and simple DSL for Espresso Compose in Kotlin Benefits Readability Reusability Extensible DSL How to use it Create Screen Create yo

null 74 Dec 26, 2022
Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3

TO-DO Notes - Code for 5.1-5.3 Testing Codelab Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3 Introduction TO-DO Notes is an app where y

Jorge M 1 Jun 7, 2022
A seed and demo about how to do end-to-end testing of a Dataflow pipeline

dataflow-e2e-demo This is a demo and a seed project to show how you can end-to-end test a Dataflow pipeline. You can find more about by follwing this

null 0 Dec 18, 2021