Unit testing using Spek framework

Kishan Maurya
ProAndroidDev
Published in
3 min readJul 16, 2020

--

In my previous articles on unit testing, I have talked about ViewModel, Usecase, Repository testing using JUnit & Mockito. Today I am going to explore about Spek framework for unit testing.

What is Spek?

Spek is a Kotlin-based Specification Testing framework. Spek is written in Kotlin and specifications or tests we write will be in Kotlin. However, our SUT (System Under Test) can be Java or Kotlin.

Currently, Spek is implemented as a JUnit Platform TestEngine. This means that we can easily plug it into any project that already uses JUnit 5.

This allows us to easily define specifications in a clear, understandable, human-readable way. This framework allows you to describe tests and expected behaviors in a more readable way.

How to start within the Android project?

  1. Add following dependencies in Build.gradle( Project level)

2. Add the following dependencies in Build.gradle( App level)

org.jetbrains.spek:spek-api is needed during compilation
org.jetbrains.spek:spek-junit-platform-engine is needed during runtime.

For writing the test cases, we need to choose which DSL style to use.
Spek offers two styles of DSL for writing test cases, namely Specification (describe -> context -> it) and Gherkin(Feature->Scenario->When->then )

We will use the Specification DSL.

{describe -> it}

The style follows the Jasmine and Mocha style frameworks, where things are defined in describe blocks.

describe: which defines the context of what we’re testing. We can nest as many describes as we want.
it: which defines the actual tests i.e what to be verified once the action is executed.

Output

Spek doesn’t have any built-in assertions. It allows us to use whatever assertion framework we’re most comfortable with eg. Kluent, Expekt, or HamKrest.

Working with Spek

Tests are written using nested lambdas, each level can either be a group or a test.

Test is where we place our assertions/checks.
Group is used to organize your tests. It can contain test and other group as well

Fixtures

Spek allows running arbitrary code before and after a group and test is executed. Spek can also execute logic before/after tests.

beforeGroup, afterGroup
beforeEachTest, afterEachTest

Spek will execute each beforeGroup block immediately before every group and each afterGroup block immediately after every group in the current nesting.

Spek will execute each beforeEachTest block immediately before every it block and each afterEachTest block immediately after every it block.

afterGroup {
clearInvocations(endpoint)
}
beforeEachTest {
endpoint = mock()
}

afterEachTest {
reset(endpoint)
}

Ignoring some Test cases :

we can ignore a test with a prefix of “x” like this. (e.g. xdescribe, xit )

xit("Should return list of comments") {}

Issues Observed :

  1. When we try to use test rule with Rule annotation, we get the following error.
    This annotation is not applicable to target ‘local variable’
@Rule
var rule: TestRule = InstantTaskExecutorRule()

Solution :

https://github.com/spekframework/spek/issues/337

--

--