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).
You’ll understand:
~ 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
2. About Knoldus
Knoldus is a technology consulting firm with focus on modernizing the digital systems
at the pace your business demands.
DevOps
Functional. Reactive. Cloud Native
3. 01 What is Spock Framework?
02 Spock Features
03 Why Spock Framework?
04 What is Specification?
05 Mocking and Stubbing
05050505
05
Our Agenda
06 JUnit vs Spock
4. What is Spock Framework?
● Spock is a BDD-style developer testing and specification framework for Java and
Groovy applications.
● 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.
5. Spock Features
● Highly expressive – Test cases can be expressed in terms of given, when and then
blocks.
● Compatible with most IDEs – It is compatible with all the leading IDEs in the
market.
● 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.
6. 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
test cases.
● Expressive than the typical Junit.
● Easy data driven testing which was very verbose in Junit
7. What is Specification?
● A specification is represented as a Groovy class that extends from
spock.lang.Specification.
● 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
build tools.