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 basic test

farenda 2015-05-29 0

Problem:

Spock Framework is fantastic unit tests library that makes people love to test their code. Check out this post and learn how to start with Spock Framework!

Spock Framework properties:

  • concise and readable tests
  • ease of mocking and defining interactions
  • good integration with Maven and Gradle
  • using Groovy, which is more expressive language than Java

Spock‘s drawbacks:

  • need to learn a new language: Groovy
  • can’t mock statics, finals, etc. (only with Powermock)
  • very slow release cycles

Solution:

Spock Framework test:

  • it’s a Groovy class and extends spock.lang.Specification,
  • has some: fields, setup, tests, helper methods,
  • instructs JUnit to execute test with Spock’s runner – Sputnik.

Example unit test:

import spock.lang.Specification

class BasicListTest extends Specification {
  def "should not be empty after adding element"() {
    given:
    // [] is Groovy literal for List and is infered
    def list = []

    when:
    list.add(42)

    then:
    // Asserts are implicit and not need to be stated.
    // Change "==" to "!=" and see what's happening!
    list.size() == 1
    list == [42]
  }
}

given, when, then are Spock‘s labels which indicate test sections as in Behavior Driven Development. The test is very readable and easy to write.

Remember to install Spock Framework plugin if you are using Intellij IDEA!

References:

Spock Framework docs

Share with the World!
Categories Spock Framework Tags unit-tests
Previous: Java 8 create temporary file
Next: Spock Framework assert helper method

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 © 2021

sponsored