Jul 21, 2017 · 1 min read
From your example it is quite clear that explicit text is better than cryptic expression. On the other side adding excessive verbosity sometimes makes things only worse. I was looking for a test framework for Kotlin recently. I have found Spek http://spekframework.org/ and I completely dislike their approach. For me JUnit looks much better.
=============================
Your typical test code
Here’s some typical test code found in many codebases
@Test
public void testCalculateTaxRate() { TaxRateCalculator calculator = new TaxRateCalculator(); Int value = calculator.calculateRate(200, 10); assertEquals(300,value);
}
This code suffers from several issues. Under what conditions is the tax rate calculated? What exactly is it doing? What is the expected outcome?
Being explicit with Spek
Spek makes it easy to define these three important aspects without resorting to long method names or underscores:
class SimpleTest : Spek({
describe("a calculator") {
val calculator = SampleCalculator() it("should return the result of adding the first number to the second number") {
val sum = calculator.sum(2, 4)
assertEquals(6, sum)
} it("should return the result of subtracting the second number from the first number") {
val subtract = calculator.subtract(4, 2)
assertEquals(2, subtract)
}
}
})
