Skip to content

Testing

brianneoberson edited this page Dec 5, 2023 · 1 revision

Example test class

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class nameOfClassThatsToTestTests{

    @Test
    fun nameOfFunctionThatsToTest_nameOfTestDataSetThatsUsed() {
        // Arrange
        val a = 3
        val b = 5

        // Act
        val result = a + b

        // Assert
        assertEquals(8, result, "Addition failed: $a + $b should be 8")
    }
}

How to acces Test Data

There are several ways to access test data:

  • Using Hardcoded Data:
@Test
fun testAddition() {
    val a = 3
    val b = 5
    val result = a + b
    assertEquals(8, result, "Addition failed: $a + $b should be 8")
}
  • Using External Configuration Files:
@Test
fun testAddition() {
    val testData = loadTestData("testdata.json")
    val result = testData.a + testData.b
    assertEquals(testData.expectedResult, result, "Addition failed")
}
  • Using Test Fixtures and Factories
@Test
fun testAddition() {
    val testData = TestDataFactory.createTestData()
    val result = testData.a + testData.b
    assertEquals(testData.expectedResult, result, "Addition failed")
}

Further Information

For further information concerning testing see: IntelliJ - Testing