# Spekulations on Testing

Brief overview of Spek and its benefits

October 11, 2016

## Transcript

4. ### @Test public void testCalculator() { SampleCalculator calculator = new SampleCalculator();

Int value = calculator.sum(2, 4); assertEquals(6, value); } Tests
5. ### • Under what conditions is sum calculated? • What exactly

is it doing? • What is the expected outcome? It could be better
6. ### Speks class SimpleSpec: Spek({ describe("a calculator") { val calculator =

SampleCalculator() . . . . . . . . . } })
7. ### Speks class SimpleSpec: 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) } . . . } })
8. ### Speks class SimpleSpec: 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) } } })
9. ### Spek • Built with by Jetbrains • Built with Kotlin

• Runs with JUnit 4 (+ 5) • Specification Framework • Can test Java or Kotlin • Open Source • IntelliJ Plugin (Android Studio)
10. ### class FixtureSpec: Spek({ describe("a group") { beforeEachTest { ... }

context("a nested group") { beforeEachTest { ... } ... Fixtures
11. ### Fixtures ... context("a nested group") { beforeEachTest { ... }

it ("should work") { ... } } it("do something") { … } afterEachTest { ... } } })
12. ### class SimpleCalculatorSpec: SubjectSpek <Calculator> ({ subject { Calculator() } (1)

it("should return the result of adding the first number to the second number") { assertEquals(6, subject.sum(2, 4)) (2) } it("should return the result of subtracting the second number from the first number") { assertEquals(2, subject.subtract(4, 2)) (2) } }) Subjects
13. ### (1) Tell Spek how to instantiate the subject, in this

case a Calculator. This will be invoked for every test scope, which means each test scope will have a unique instance. (2) Use subject to access the instance of the subject. Subjects

15. ### • Dependency on Kotlin, which means they’re not always in

sync. Caveats
16. ### • Dependency on Kotlin, which means they’re not always in

sync. • Studio Plugin isn’t 100% working, can be a bit flaky Caveats
17. ### • Dependency on Kotlin, which means they’re not always in

sync. • Studio Plugin isn’t 100% working, can be a bit flaky • Mockito! ◦ Null issues due to Kotlin’s nullability protection ▪ Use Mockito-Kotlin (https://github.com/nhaarman/mockito-kotlin) • Depends on Mockito 2.1.0 ◦ 2.1.0 was just released last month... Caveats