Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Spock Framework before after class

farenda 2015-06-17 2

Problem:

How to run initialization code before and/or after class/specification in Spock Framework? As always, examples will clarify things out!

Solution:

In Spock Framework you can run initialization for the whole Specification/class in setupSpec() method and do cleanup in cleanupSpec(). The methods have to be named in this way.

Here’s an example:

package com.farenda.solved

import spock.lang.Shared
import spock.lang.Specification

class BeforeAfterSpecTest extends Specification {

    // Must be @Shared or static!
    def @Shared VeryCostlyObject heavy

    // Run before all the tests:
    def setupSpec() {
        println("Heavy init for all the tests...")
        heavy = new VeryCostlyObject()
        println("Initialization done!")
    }

    // Run after all the tests, even after failures:
    def cleanupSpec() {
        println("Cleanup after all tests!")
    }

    def 'should create a range of 5 elements'() {
        println('Inside the range test.')
        expect:
        (1..5).size() == 5
    }

    def 'should miscount items in list'() {
        println('(mis)counting items in a list test')
        expect:
        [1, 2, 3].size() == 4 // lets fail this one!
    }
}

Beware that setupSpec() and cleanupSpec() can operate only on static and @Shared fields, because they are executed before tests get even instantiated. You can think of them as static methods on Specification, so they work outside an instance, similarly to JUnit‘s @BeforeClass and @AfterClass annotation.

If you try to assign to non-shared or non-static field you will get an error Only @Shared and static fields may be accessed from here!

Running the test gives the following output:

Heavy init for all the tests...
Instantiating very costly object...
...5 minutes later ;-)
Done!
Initialization done!
Inside the range test.
(mis)counting items in a list test

Condition not satisfied:

[1, 2, 3].size() == 4 // lets fail this one!
          |      |
          3      false
 <Click to see difference>

      at com.farenda.solved.BeforeAfterSpecTest.should count items in list(BeforeAfterSpecTest.groovy:32)

Cleanup after all tests!

Process finished with exit code 255

As you can see setupSpec() and cleanupSpec() have been called only once for all the tests. The thing to note is that cleanupSpec() has been called even when the test failed, allowing us to dispose resources.

Have fun! :-)

Share with the World!
Categories Spock Framework Tags unit-tests
Previous: Spock Framework before after test
Next: Gradle System.out err print

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok