From f8db7b02c5d8e4f7e9f786f69fd2705f1415d10c Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Wed, 11 Oct 2017 02:44:50 +0100 Subject: [PATCH] Kotlin junit5 (#2672) * Fixed the core-kotlin module to build, and set it up to run JUnit 5 tests using Failsafe * Example JUnit5 tests in Kotlin --- core-kotlin/pom.xml | 56 ++++++++++++- .../com/baeldung/kotlin/junit5/Calculator.kt | 17 ++++ .../baeldung/kotlin/junit5/CalculatorTest5.kt | 82 +++++++++++++++++++ .../kotlin/junit5/DivideByZeroException.kt | 3 + .../com/baeldung/kotlin/junit5/SimpleTest5.kt | 21 +++++ 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 856a37ded006..e795d1e04201 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -5,6 +5,7 @@ core-kotlin 1.0-SNAPSHOT + jar com.baeldung @@ -20,6 +21,24 @@ + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + junit + junit + ${junit4.version} + test + org.jetbrains.kotlin kotlin-stdlib @@ -116,16 +135,51 @@ + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + maven-failsafe-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + UTF-8 1.1.2 1.1.2 1.1.2 1.1.2 0.15 1.5.0 + + 5.0.0 + 1.0.0 + 4.12.0 + 4.12 - \ No newline at end of file + diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt new file mode 100644 index 000000000000..1b61c05887e4 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt @@ -0,0 +1,17 @@ +package com.baeldung.kotlin.junit5 + +class Calculator { + fun add(a: Int, b: Int) = a + b + + fun divide(a: Int, b: Int) = if (b == 0) { + throw DivideByZeroException(a) + } else { + a / b + } + + fun square(a: Int) = a * a + + fun squareRoot(a: Int) = Math.sqrt(a.toDouble()) + + fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble()) +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt new file mode 100644 index 000000000000..dd358050441c --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt @@ -0,0 +1,82 @@ +package com.baeldung.kotlin.junit5 + +import org.junit.jupiter.api.* +import org.junit.jupiter.api.function.Executable + +class CalculatorTest5 { + private val calculator = Calculator() + + @Test + fun testAddition() { + Assertions.assertEquals(4, calculator.add(1, 3)) + } + + @Test + fun testDivideByZero() { + val exception = Assertions.assertThrows(DivideByZeroException::class.java) { + calculator.divide(5, 0) + } + + Assertions.assertEquals(5, exception.numerator) + } + + @Test + fun testSquares() { + Assertions.assertAll( + Executable { Assertions.assertEquals(1, calculator.square(1)) }, + Executable { Assertions.assertEquals(4, calculator.square(2)) }, + Executable { Assertions.assertEquals(9, calculator.square(3)) } + ) + } + + @TestFactory + fun testSquaresFactory() = listOf( + DynamicTest.dynamicTest("1 squared") { Assertions.assertEquals(1,calculator.square(1))}, + DynamicTest.dynamicTest("2 squared") { Assertions.assertEquals(4,calculator.square(2))}, + DynamicTest.dynamicTest("3 squared") { Assertions.assertEquals(9,calculator.square(3))} + ) + + @TestFactory + fun testSquaresFactory2() = listOf( + 1 to 1, + 2 to 4, + 3 to 9, + 4 to 16, + 5 to 25) + .map { (input, expected) -> + DynamicTest.dynamicTest("$input squared") { + Assertions.assertEquals(expected, calculator.square(input)) + } + } + + private val squaresTestData = listOf( + 1 to 1, + 2 to 4, + 3 to 9, + 4 to 16, + 5 to 25) + + @TestFactory + fun testSquaresFactory3() = squaresTestData + .map { (input, expected) -> + DynamicTest.dynamicTest("$input squared") { + Assertions.assertEquals(expected, calculator.square(input)) + } + } + @TestFactory + fun testSquareRootsFactory3() = squaresTestData + .map { (expected, input) -> + DynamicTest.dynamicTest("Square root of $input") { + Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input)) + } + } + + @Tags( + Tag("slow"), + Tag("logarithms") + ) + @Test + fun testLogarithms() { + Assertions.assertEquals(3.0, calculator.log(2, 8)) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt new file mode 100644 index 000000000000..60bc4e2944e5 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.junit5 + +class DivideByZeroException(val numerator: Int) : Exception() diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt new file mode 100644 index 000000000000..c04ab568f7f9 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt @@ -0,0 +1,21 @@ +package com.baeldung.kotlin.junit5 + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test + +class SimpleTest5 { + @Test + fun testEmpty() { + val list = listOf() + Assertions.assertTrue(list::isEmpty) + } + + @Test + @Disabled + fun testMessage() { + Assertions.assertEquals(3, 4) { + "Three does not equal four" + } + } +}