Scala supports testing using the JUnit and ScalaTest frameworks. These frameworks provide a way to write and run tests for your Scala code. JUnit is a popular choice for unit testing in Scala 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.Test

class CalculatorTest {
    @Test
    def addShouldReturnSumOfTwoNumbers(): Unit = {
        val calculator = new Calculator()
        val result = calculator.add(2, 3)
        assertEquals(5, result)
    }
}

Running Tests

To run the tests, use the sbt command-line interface.

sbt test

To run specific test files, use the testOnly command.

sbt "testOnly CalculatorTest"