Getting used to jUnit5

Hi, I’m new to Kotlin and trying to get a simple test-application within IntelliJ (commiunity edition)to work.

So I created the following files:
/src/main/kotlin/com/example/calculator
Calculator.kt

package com.example.calculator

class Calculator {

    fun add(a: Double, b: Double): Double {
        return a + b
    }

    fun multiply(a: Double, b: Double): Double {
        return a * b
    }
}

Main.kt

package com.example.calculator

fun main() {
    val calculator = Calculator()

    val num1 = 5.0
    val num2 = 3.0

    val additionResult = calculator.add(num1, num2)
    val multiplicationResult = calculator.multiply(num1, num2)

    println("Addition: $num1 + $num2 = $additionResult")
    println("Multiplikation: $num1 * $num2 = $multiplicationResult")
}

src/test/kotlin/com/example/calculator/CalculatorTest.kt:

package com.example.calculator

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

class CalculatorTest {

    private val calculator = Calculator()

    @ParameterizedTest
    @CsvSource(
        "1.0, 2.0, 3.0",
        "-1.0, -2.0, -3.0",
        "0.0, 0.0, 0.0",
        "1.5, -2.5, -1.0",
        "-3.0, 3.0, 0.0"
    )
    fun testAddition(a: Double, b: Double, expected: Double) {
        assertEquals(expected, calculator.add(a, b))
    }

    @ParameterizedTest
    @CsvSource(
        "1.0, 2.0, 2.0",
        "-1.0, -2.0, 2.0",
        "0.0, 5.0, 0.0",
        "1.5, -2.5, -3.75",
        "-3.0, 3.0, -9.0"
    )
    fun testMultiplication(a: Double, b: Double, expected: Double) {
        assertEquals(expected, calculator.multiply(a, b))
    }
}

But IntelliJ keeps telling me:
/src/test/kotlin/com/example/calculator/CalculatorTest.kt:4:26 Unresolved reference: params

So what might I have done wrong?
Trying to get this working since days - even ChatGPT couldn’t help me so far.

Also I couldn’t find any tutorial (including Kotlin, IntelliJ, gradle and jUnit5) that is usefol for me as beginner :frowning:

I guess you miss the dependency for the library: org.junit.jupiter:junit-jupiter-params. Check your gradle config for existing junit dependencies and add the above in a similar way.

1 Like

Wow, thanks a lot, now it works :slight_smile:

Oh, just to keep everything together, here’s what my (now working!) build.gradle.kts looks like:

plugins {
    kotlin("jvm") version "1.8.0"
    application
}

application {
    mainClass.set("com.example.calculator.MainKt")
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-params")        // hat mich tagelang aufgehalten
    testImplementation(kotlin("test"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(17)
}

1 Like