LinkedIn emplea cookies para mejorar la funcionalidad y el rendimiento de nuestro sitio web, así como para ofrecer publicidad relevante. Si continúas navegando por ese sitio web, aceptas el uso de cookies. Consulta nuestras Condiciones de uso y nuestra Política de privacidad para más información.
LinkedIn emplea cookies para mejorar la funcionalidad y el rendimiento de nuestro sitio web, así como para ofrecer publicidad relevante. Si continúas navegando por ese sitio web, aceptas el uso de cookies. Consulta nuestra Política de privacidad y nuestras Condiciones de uso para más información.
When it comes to true unit tests, having a mocking framework such as Spock for Java is essential. Using a mocking framework ensures that your unit tests are fast, self-contained, and deterministic. A mocking framework can help you fake external systems, pre-program your classes with expected responses, and test hard-to-replicate error conditions.
In this, we will cover the Spock testing framework, an emerging framework that can be used to test both Java and Groovy code. Unlike Mockito, Spock is a complete testing framework that can function on its own (Mockito works in addition to JUnit).
~ How Spock tests leverage existing JUnit infrastructure
~ Spock annotations and where they apply
~ Spock mocks and stubs and how to take advantage of them
And you’ll be able to:
~ Set up test fixtures
~ Run testing methods before and after tests and test classes
~ Mock classes and interfaces set expectations, and validate them
A Unit Testing
Presented By: Deepak Mehra
Senior Software Consultant
Knoldus is a technology consulting firm with focus on modernizing the digital systems
at the pace your business demands.
Functional. Reactive. Cloud Native
01 What is Spock Framework?
02 Spock Features
03 Why Spock Framework?
04 What is Specification?
05 Mocking and Stubbing
06 JUnit vs Spock
What is Spock Framework?
● Spock is a BDD-style developer testing and specification framework for Java and
● Spock combines unit testing, mocking stubbing and business driven development
all into one framework so you don’t have to download/include additional libraries
in your build.
● Everything is inbuilt in Spock framework, it was inspired by JUnit framework only
and JUnit is also used under the hood.
● We will write the code in Java but we will test the code in Groovy.
● Highly expressive – Test cases can be expressed in terms of given, when and then
● Compatible with most IDEs – It is compatible with all the leading IDEs in the
● Compatible with build tools – Compatible with Maven as well as Gradle.
● Compatible with Continuous integration servers – The JUnit runner makes it
compatible with all the continuous integration servers out there.
Why Spock Framework?
● Highly Expressive Specification Language.
● Compatibility with leading IDEs.
● Test cases are written in Groovy which is pretty much similar to Java and also a
JVM based language.
● One framework for all types of test cases, no other libraries are required to write
● Expressive than the typical Junit.
● Easy data driven testing which was very verbose in Junit
What is Specification?
● A specification is represented as a Groovy class that extends from
● The name of a specification usually relates to the system or system operation
described by the specification.
● Class Specification contains a number of useful methods for writing specifications.
Furthermore it instructs JUnit to run specification with Sputnik, Spock’s JUnit runner.
● Thanks to Sputnik, Spock specifications can be run by most modern Java IDEs and