The Kotlin test libraries

In this section, we will look at the Spek testing framework (https://spekframework.github.io/spek/docs/latest/#_what_is_spek) for Kotlin. Spek, contrary to other frameworks, such as JUnit, provides a way to describe requirements along with a test script. The fact that tests have passed successfully, doesn't mean that our code works as expected and meets our requirements.

As you can see in the previous example, we should use class and method names to describe a case that we want to test. Using the Spek framework, we can rewrite this example as follows:

object CalculatorSpec: Spek({
given("a calculator") {
val calculator = Calculator()
on("addition") {
val sum = calculator.addition(3, 2)
it("adding 3 and 2 should return 5") {
assertEquals(5, sum)
}
}
}
})

Spek is written in Kotlin because this language provides many advantages over Java, such as the ability to use domain-specific language (DSL). A specification should also be written in Kotlin, but you can test your Java code just as Kotlin because this is designed to interoperate with Java. Spek, by specification, refers to a class with tests because it looks like a specification and is written in a declarative manner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.94