Skip to content

Instantly share code, notes, and snippets.

@x0000ff
Created March 12, 2020 18:59
Show Gist options
  • Save x0000ff/56ca8143549889e3f3e16cd024e2beef to your computer and use it in GitHub Desktop.
Save x0000ff/56ca8143549889e3f3e16cd024e2beef to your computer and use it in GitHub Desktop.

Configure Tests for Kotlin Project

Create project

  1. In IntelliJ Idea: File => New => Project

  2. Select type Gradle and in Additional LIbriries and Framworks select only Kotlin/JVM

    created

Add JUnit and Kotlin JUnit

Open build.gradle and add to dependencies block this lines:

testImplementation 'junit:junit:4'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"

Here you can see my build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.6"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.6"
}

Add class for test

Inside src => main => kotlin create file Hello.kt

package main

class Hello {
    fun getGreeting(): String {
        return "Hello"
    }
}

Add test

Inside src => test => kotlin create file HelloTest.kt

import main.Hello

import kotlin.test.assertEquals
import org.junit.Test as test

class HelloTest {
    @test
    fun simpleTest() {
        val greeting = Hello().getGreeting()
        assertEquals(greeting, "Hola")
    }
}

Run failed

failed

Run succeed

succeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment