Writing tests in Kotlin can be done using the JUnit and KotlinTest frameworks. These frameworks provide a way to write and run tests for your Kotlin code. JUnit is a popular choice for unit testing in Kotlin due to its simplicity and extensibility.
Writing Tests
JUnit provides a set of annotations that can be used to write tests. The @Test annotation is used to mark a method as a test method. The @Before and @After annotations are used to mark methods that should be run before and after each test method, respectively.
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
class CalculatorTest {
@Test
fun `add should return sum of two numbers`() {
val calculator = Calculator()
val result = calculator.add(2, 3)
assertEquals(5, result)
}
}