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
This commit is contained in:
parent
9c0b6b4a47
commit
f8db7b02c5
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
<artifactId>core-kotlin</artifactId>
|
<artifactId>core-kotlin</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
@ -20,6 +21,24 @@
|
|||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<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>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-stdlib</artifactId>
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
@ -116,16 +135,51 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
|
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
|
||||||
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
|
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
|
||||||
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
|
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
|
||||||
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
|
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
|
||||||
<kotlinx.version>0.15</kotlinx.version>
|
<kotlinx.version>0.15</kotlinx.version>
|
||||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -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())
|
||||||
|
}
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.baeldung.kotlin.junit5
|
||||||
|
|
||||||
|
class DivideByZeroException(val numerator: Int) : Exception()
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user