Skip to content

Commit f8db7b0

Browse files
sazzerKevinGilmore
authored andcommitted
Kotlin junit5 (eugenp#2672)
* Fixed the core-kotlin module to build, and set it up to run JUnit 5 tests using Failsafe * Example JUnit5 tests in Kotlin
1 parent 9c0b6b4 commit f8db7b0

5 files changed

Lines changed: 178 additions & 1 deletion

File tree

‎core-kotlin/pom.xml‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
<artifactId>core-kotlin</artifactId>
77
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
89

910
<parent>
1011
<groupId>com.baeldung</groupId>
@@ -20,6 +21,24 @@
2021
</repositories>
2122

2223
<dependencies>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<version>${junit.jupiter.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.platform</groupId>
32+
<artifactId>junit-platform-runner</artifactId>
33+
<version>${junit.platform.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<version>${junit4.version}</version>
40+
<scope>test</scope>
41+
</dependency>
2342
<dependency>
2443
<groupId>org.jetbrains.kotlin</groupId>
2544
<artifactId>kotlin-stdlib</artifactId>
@@ -116,16 +135,51 @@
116135
</execution>
117136
</executions>
118137
</plugin>
138+
<plugin>
139+
<artifactId>maven-surefire-plugin</artifactId>
140+
<version>${maven-surefire-plugin.version}</version>
141+
</plugin>
142+
<plugin>
143+
<artifactId>maven-failsafe-plugin</artifactId>
144+
<version>2.19.1</version>
145+
<dependencies>
146+
<dependency>
147+
<groupId>org.junit.platform</groupId>
148+
<artifactId>junit-platform-surefire-provider</artifactId>
149+
<version>${junit.platform.version}</version>
150+
</dependency>
151+
</dependencies>
152+
<executions>
153+
<execution>
154+
<id>junit5</id>
155+
<goals>
156+
<goal>integration-test</goal>
157+
<goal>verify</goal>
158+
</goals>
159+
<configuration>
160+
<includes>
161+
<include>**/*Test5.java</include>
162+
</includes>
163+
</configuration>
164+
</execution>
165+
</executions>
166+
</plugin>
119167
</plugins>
120168
</build>
121169

122170
<properties>
171+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
123172
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
124173
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
125174
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
126175
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
127176
<kotlinx.version>0.15</kotlinx.version>
128177
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
178+
179+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
180+
<junit.platform.version>1.0.0</junit.platform.version>
181+
<junit.vintage.version>4.12.0</junit.vintage.version>
182+
<junit4.version>4.12</junit4.version>
129183
</properties>
130184

131-
</project>
185+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.kotlin.junit5
2+
3+
class Calculator {
4+
fun add(a: Int, b: Int) = a + b
5+
6+
fun divide(a: Int, b: Int) = if (b == 0) {
7+
throw DivideByZeroException(a)
8+
} else {
9+
a / b
10+
}
11+
12+
fun square(a: Int) = a * a
13+
14+
fun squareRoot(a: Int) = Math.sqrt(a.toDouble())
15+
16+
fun log(base: Int, value: Int) = Math.log(value.toDouble()) / Math.log(base.toDouble())
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.baeldung.kotlin.junit5
2+
3+
import org.junit.jupiter.api.*
4+
import org.junit.jupiter.api.function.Executable
5+
6+
class CalculatorTest5 {
7+
private val calculator = Calculator()
8+
9+
@Test
10+
fun testAddition() {
11+
Assertions.assertEquals(4, calculator.add(1, 3))
12+
}
13+
14+
@Test
15+
fun testDivideByZero() {
16+
val exception = Assertions.assertThrows(DivideByZeroException::class.java) {
17+
calculator.divide(5, 0)
18+
}
19+
20+
Assertions.assertEquals(5, exception.numerator)
21+
}
22+
23+
@Test
24+
fun testSquares() {
25+
Assertions.assertAll(
26+
Executable { Assertions.assertEquals(1, calculator.square(1)) },
27+
Executable { Assertions.assertEquals(4, calculator.square(2)) },
28+
Executable { Assertions.assertEquals(9, calculator.square(3)) }
29+
)
30+
}
31+
32+
@TestFactory
33+
fun testSquaresFactory() = listOf(
34+
DynamicTest.dynamicTest("1 squared") { Assertions.assertEquals(1,calculator.square(1))},
35+
DynamicTest.dynamicTest("2 squared") { Assertions.assertEquals(4,calculator.square(2))},
36+
DynamicTest.dynamicTest("3 squared") { Assertions.assertEquals(9,calculator.square(3))}
37+
)
38+
39+
@TestFactory
40+
fun testSquaresFactory2() = listOf(
41+
1 to 1,
42+
2 to 4,
43+
3 to 9,
44+
4 to 16,
45+
5 to 25)
46+
.map { (input, expected) ->
47+
DynamicTest.dynamicTest("$input squared") {
48+
Assertions.assertEquals(expected, calculator.square(input))
49+
}
50+
}
51+
52+
private val squaresTestData = listOf(
53+
1 to 1,
54+
2 to 4,
55+
3 to 9,
56+
4 to 16,
57+
5 to 25)
58+
59+
@TestFactory
60+
fun testSquaresFactory3() = squaresTestData
61+
.map { (input, expected) ->
62+
DynamicTest.dynamicTest("$input squared") {
63+
Assertions.assertEquals(expected, calculator.square(input))
64+
}
65+
}
66+
@TestFactory
67+
fun testSquareRootsFactory3() = squaresTestData
68+
.map { (expected, input) ->
69+
DynamicTest.dynamicTest("Square root of $input") {
70+
Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input))
71+
}
72+
}
73+
74+
@Tags(
75+
Tag("slow"),
76+
Tag("logarithms")
77+
)
78+
@Test
79+
fun testLogarithms() {
80+
Assertions.assertEquals(3.0, calculator.log(2, 8))
81+
}
82+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.junit5
2+
3+
class DivideByZeroException(val numerator: Int) : Exception()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.kotlin.junit5
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Disabled
5+
import org.junit.jupiter.api.Test
6+
7+
class SimpleTest5 {
8+
@Test
9+
fun testEmpty() {
10+
val list = listOf<String>()
11+
Assertions.assertTrue(list::isEmpty)
12+
}
13+
14+
@Test
15+
@Disabled
16+
fun testMessage() {
17+
Assertions.assertEquals(3, 4) {
18+
"Three does not equal four"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)