Skip to content

Commit

Permalink
Kotlin junit5 (eugenp#2672)
Browse files Browse the repository at this point in the history
* Fixed the core-kotlin module to build, and set it up to run JUnit 5 tests using Failsafe

* Example JUnit5 tests in Kotlin
  • Loading branch information
sazzer authored and KevinGilmore committed Oct 11, 2017
1 parent 9c0b6b4 commit f8db7b0
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 1 deletion.
56 changes: 55 additions & 1 deletion core-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<artifactId>core-kotlin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>com.baeldung</groupId>
Expand All @@ -20,6 +21,24 @@
</repositories>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
Expand Down Expand Up @@ -116,16 +135,51 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>junit5</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*Test5.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
<kotlinx.version>0.15</kotlinx.version>
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>

<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.vintage.version>4.12.0</junit.vintage.version>
<junit4.version>4.12</junit4.version>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -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())
}
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung.kotlin.junit5

class DivideByZeroException(val numerator: Int) : Exception()
Original file line number Diff line number Diff line change
@@ -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<String>()
Assertions.assertTrue(list::isEmpty)
}

@Test
@Disabled
fun testMessage() {
Assertions.assertEquals(3, 4) {
"Three does not equal four"
}
}
}

0 comments on commit f8db7b0

Please sign in to comment.