forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
9c0b6b4
commit f8db7b0
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/Calculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
82 changes: 82 additions & 0 deletions
82
core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/CalculatorTest5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/DivideByZeroException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
21 changes: 21 additions & 0 deletions
21
core-kotlin/src/test/kotlin/com/baeldung/kotlin/junit5/SimpleTest5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |