Kotlin Spek

Budi Oktaviyan
4 min readMay 8, 2018

Today, I want to introduce you to an awesome unit testing framework that written by JetBrains team. We called it as SPEK 👍 So, What is it ? It’s a Kotlin Specification framework for the JVM, Used to be a kind of new unit testing based on TDD (Test driven development) and BDD (Behavior driven development) concept. As far as I Know (AFAIK), Spek run well with jUnit5 libraries.

Like any other unit testing framework, Spek brings a default step on writing a test, but with a declarative way 😃 … before going further, we have to define dependencies that we need to use it. At this point, I will show you how we create an android unit test using Spek framework. Since jUnit5 are not supported for android development (because, android are stuck on jUnit4 😆), so we have to add a 3rd party plugin to enabled that, in our gradle root;

dependencies {
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.32"
}

and… don’t forget to add repository url to import Spek test framework;

allprojects {
repositories {
maven { url "http://dl.bintray.com/jetbrains/spek" }
}
}

because… our jUnit5 are depends on 3rd party plugin, so we have to applied that also in our gradle module;

apply plugin: 'de.mannodermaus.android-junit5'

after that… enabled the android.testOptions to bind spek framework into jUnit5;

android.testOptions {
junitPlatform {
filters {
engines {
include 'spek'
}
}
}
}

then… add required dependencies to run Spek as well;

dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.junit.platform:junit-platform-engine:1.2.0'
testImplementation 'org.jetbrains.spek:spek-api:1.1.5'
testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
}

finally, let’s cook this!

Ok, after we have finished to include all dependencies… we can create our test now. Let say, we have a class with a bunch of function like this;

class Calculator {

fun sum(x: Int, y: Int): Int = x.plus(y)
fun subtract(x: Int, y: Int) = x.minus(y)
fun multiple(x: Int, y: Int) = x.times(y)
fun divide(x: Int, y: Int) = x.div(y)
}

then… we create a test to verify if our logic are run as well. Just like an usual unit test framework, we have to create a test case for that. Define a test case that extends from Spek;

class CalculatorTest : Spek({})

In the test case, we usually define a step to test our logic;

  • Given the test case/problem statement
  • Doing the test case to our function
  • Verify that test case against to our logic

So, in Spek we can define it easily by using a BDD concept… wait, BDD ? yeah this is what Spek are offer dude! combining TDD and BDD together!

hmm, what is it looks like ? here a short sample to show that;

class CalculatorTest : Spek({

given("Some calculator") {
val calculator = Calculator()

on("Addition") {
val sum = calculator.sum(4, 4)

it("Should return the addition between first and second number") {
assertEquals(8, sum)
}
}

on("Subtraction") {
val subtract = calculator.subtract(10, 3)

it("Should return the subtraction between first and second number") {
assertEquals(7, subtract)
}
}

on("Multiplying") {
val multiple = calculator.multiple(3, 3)

it("Should return the multiplying between first and second number") {
assertEquals(9, multiple)
}
}

on("Dividing") {
val divide = calculator.divide(20, 2)

it("Should return the dividing between first and second number") {
assertEquals(10, divide)
}
}
}
}
)

Cool, right ? 😆 … If we run on our IDE, we can see our test are more readable now;

Android unit test result using spek test framework

Alright, even if you amaze with Spek… there are still some small issue that I’ve been faced, since I’m trying to use Spek;

  • If you are (still) using Android Studio version 3.1.1… there are some bugs on IDE that makes your unit test are not run as well (Fortunately, this issue already solved on latest version of Android Studio 3.1.2) 😃
  • Like what I’ve said, Spek runs well on jUnit5… while Android are not supporting, especially if you are running on IDE (not using CLI). So, we have to add a 3rd party plugin to enabled that
  • AFAIK, you’re still need a mockito dependencies to mock your object, because Spek are not build for that 😢

Ok, cool guys! That’s all from my side… telling a short story about Spek. If you need any example, you can check on my github

See ya!

--

--

Budi Oktaviyan

The only way to do great work, is to love what you do