BDD frameworks (part 3)

published in Agile Methodology, Android Development, Project Management, Tutorials
by Wojciech Szóstak

In this series of articles, I am presenting selected BDD frameworks. In this part, I will talk about Spock – a unit testing framework.

Writing unit tests with Spock framework

Spock is a framework in which the test are written with a language called Groovy. Here the natural language layer is not separated from the logic that is responsible for the test activity, but possesses the keywords given, when, and then, which can help you understand the test.

Test definition in Spock

In the Spock framework, a testing class must inherit from the Specification class:


class CalculateAreaTest extends Specification {
} 

The test begins with the phrase def:


def "Testing methods area of circle"() {
    given: "init"
    def calculateArea = new CalculateArea()
    def radius = 2
    def expectedValue = 12.566370614359172
    when: "Calculation of the circle"
    def result = calculateArea.areaOfCircle(radius)
    then: "Check result"
    result == expectedValue
}

Data Table – testing several values

An interesting option we can use here is the Data Table. Input and output of the test can be provided in the table. So that we can simultaneously check several values in one test.


def "Testing methods area of circle - Data Tables"(int radius, double expectedValue) {
    given: "init"
    def calculateArea = new CalculateArea()
    expect: "Checking the value from the table"
    calculateArea.areaOfCircle(radius) == expectedValue
    where: "Values for the variables"
    radius | expectedValue
         1 | 3.141592653589793
         7 | 153.93804002589985
         0 | 0
}

Testing the calling of a method

Another useful feature of Spock is to test whether a particular method has been performed. This function may be useful to us when the method we are testing comes from some interface and we have to check if it has been called a certain number of times.


def "Testing methods area of square"() {
    given: "init"
    def calculateArea = new CalculateArea()
    def valueX = 2
    def valueY = 2
    def listener = Mock(OnSquareListener)
    def expectedValue = 4
    when: "Calculation of the square"
    calculateArea.areaOfSquare(valueX, valueY, listener)
    then: "Verify that appropriate methods are called"
        1 * listener.onSuccess(expectedValue)
        0 * listener.onFailed()
}

In the code "quantity" determines how many times a given method should be called: quantity * object.method().

Additional elements

Sometimes we need additional Android elements. Such as context, for testing. For such needs, we can use additional annotations:

  • @UseActivity(MyActivity)
  • @UseApplication(MyApplication)
  • @WithContext

Example use of annotations:


class ContextTest extends Specification {
    @WithContext
    def context
    def "Test context"() {
        expect:
        context != null
    }
}

It should be remembered that when we want to use Android elements, a test needs to be placed in the directory: app/src/androidTest/groovy.

However, when we do not need these elements, we put the files in the directory: app/src/test/groovy. All files have the extension .groovy.

The installation of the Android elements proceeds as follows:


buildscript {
  repositories {
    jcenter()
  }

  dependencies {
     classpath 'com.android.tools.build:gradle:2.2.2'
     classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.1.0'
  }
}

Next:


apply plugin: 'com.android.application'
apply plugin: 'groovyx.android'


android {

  defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }

  packagingOptions {
    exclude 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation'
    exclude 'LICENSE.txt'
  }
}

dependencies {
//spock without components Android
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'

//spock with elements of Android
    androidTestCompile 'org.codehaus.groovy:groovy:2.4.4:grooid'
    androidTestCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude group: 'org.codehaus.groovy'
        exclude group: 'junit'
    }
    androidTestCompile 'com.andrewreitz:spock-android:1.2.2'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
}

BDD frameworks

Behavior-Driven Development is a useful process for testing application functionality.

Thanks to BDD testing tools like Calabash used with the Cucumber framework or Green Coffee based on Gherkin, everyone involved in the project can easily verify the views of the Android app being designed. And check if the functionalities fulfill the criteria for acceptance that were stated in the requirements.

The Spock framework helps Unit testing be performed efficiently. Now the communication between programmer, manager and customer will be clear, and automated software testing will end in success!

Check our previous article: BDD frameworks (part 2).


You can find the source code on my GitHub.


Wojciech Szóstak Mobile Developer

Wojtek is a mobile developer at Zaven. He mostly works on android apps and enhances his knowledge about software testing (like BDD). In his spare time, Wojtek enjoys martial arts - he exercises several times a week!

Popular posts

Clutch Announces Zaven as Top Polish Development in  2019 Eastern European Leaders Award

Clutch Announces Zaven as Top Polish Development in 2019 Eastern European Leaders Award

Since 2011, Zaven has been providing digital solutions for global clients. Our team is composed of experts in custom development, UX/UI design, data analytics, backend integration and more. We aim to build long-term business relationships with our clients in order to create the highest quality and most innovative solutions. Clutch, a B2B market research platform, […]

Read more
How to overcame the fear of hiring outsourced developers?

How to overcame the fear of hiring outsourced developers?

There is many a legend about outsourcing which successfully keeps away entrepreneurs. Stories about software which never even existed, despite the fact that the developer had promised that everything was fine - even ahead of deadline. No communication with the developer for a long, long time and then it turns out that nobody has been working because it is Chinese New Year! These are telling examples and they are not isolated. I can only imagine the fear that grips some people when the subject of outsourcing steps out of the shadows! Let’s just deal with the most common fears of hiring outsourced developers.

Read more
Why we moved to Kotlin for Android development (and maybe you should too)

Why we moved to Kotlin for Android development (and maybe you should too)

Recently Kotlin programming is really endorsed by Google and most of the developers. We are not an exception. We moved to Kotlin for Android development and it was worth it. Here are 8 reasons why we did it.

Read more
Mobile Apps

Get your mobile app in 3 easy steps!

1

Spec out

with the help of our
business analyst

2

Develop

design, implement
and test, repeat!

3

Publish

get your app out
to the stores


back to top