1. JUnit 5
kotlin
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
class ExampleTest {
@Test
fun additionTest() {
val result = 2 + 2
assertEquals(4, result)
}
}
2. Spek
kotlin
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
import org.spekframework.spek2.style.specification.describe
import kotlin.test.assertEquals
object ExampleSpec : Spek({
feature("Math operations") {
scenario("Addition") {
given("two numbers") {
val a = 2
val b = 2
val expected = 4
When("adding the numbers") {
val result = a + b
Then("the result should be the expected sum") {
assertEquals(expected, result)
}
}
}
}
}
})
3. MockK
kotlin
import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertEquals
class ExampleTest {
@Test
fun mockExampleTest() {
val mock = mockk<Calculator>()
every { mock.add(2, 2) } returns 4
val result = mock.add(2, 2)
assertEquals(4, result)
}
}
class Calculator {
fun add(a: Int, b: Int): Int {
return a + b
}
}