JAVA-101 Split Core Kotlin

This commit is contained in:
mikr
2020-06-08 00:22:04 +02:00
parent 0130ae560f
commit 73e169d1eb
48 changed files with 136 additions and 27 deletions

View File

@@ -0,0 +1,6 @@
## Core Kotlin Testing
This module contains articles about testing in Kotlin
### Relevant articles:
- [JUnit 5 for Kotlin Developers](https://www.baeldung.com/junit-5-kotlin)

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-kotlin-testing</artifactId>
<name>core-kotlin-testing</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-kotlin-modules</groupId>
<artifactId>core-kotlin-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<junit.platform.version>1.1.1</junit.platform.version>
</properties>
</project>

View File

@@ -0,0 +1,17 @@
package com.baeldung.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())
}

View File

@@ -0,0 +1,82 @@
package com.baeldung.junit5
import org.junit.jupiter.api.*
import org.junit.jupiter.api.function.Executable
class CalculatorUnitTest {
private val calculator = Calculator()
@Test
fun `Adding 1 and 3 should be equal to 4`() {
Assertions.assertEquals(4, calculator.add(1, 3))
}
@Test
fun `Dividing by zero should throw the DivideByZeroException`() {
val exception = Assertions.assertThrows(DivideByZeroException::class.java) {
calculator.divide(5, 0)
}
Assertions.assertEquals(5, exception.numerator)
}
@Test
fun `The square of a number should be equal to that number multiplied in itself`() {
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("when I calculate 1^2 then I get 1") { Assertions.assertEquals(1,calculator.square(1))},
DynamicTest.dynamicTest("when I calculate 2^2 then I get 4") { Assertions.assertEquals(4,calculator.square(2))},
DynamicTest.dynamicTest("when I calculate 3^2 then I get 9") { 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("when I calculate $input^2 then I get $expected") {
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("when I calculate $input^2 then I get $expected") {
Assertions.assertEquals(expected, calculator.square(input))
}
}
@TestFactory
fun testSquareRootsFactory3() = squaresTestData
.map { (expected, input) ->
DynamicTest.dynamicTest("I calculate the square root of $input then I get $expected") {
Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input))
}
}
@Tags(
Tag("slow"),
Tag("logarithms")
)
@Test
fun `Log to base 2 of 8 should be equal to 3`() {
Assertions.assertEquals(3.0, calculator.log(2, 8))
}
}

View File

@@ -0,0 +1,3 @@
package com.baeldung.junit5
class DivideByZeroException(val numerator: Int) : Exception()

View File

@@ -0,0 +1,22 @@
package com.baeldung.junit5
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
class SimpleUnitTest {
@Test
fun `isEmpty should return true for empty lists`() {
val list = listOf<String>()
Assertions.assertTrue(list::isEmpty)
}
@Test
@Disabled
fun `3 is equal to 4`() {
Assertions.assertEquals(3, 4) {
"Three does not equal four"
}
}
}