CommunityTrisha Gee

Trisha Gee

Java Champion

Writing Tests with Spock

Tutorial

Writing Tests with Spock
Learn about the Spock testing framework by creating a project that uses Spock to unit test Java code.
A Simple Assertion

Part of tutorial

A Simple Assertion
Writing our first Spock test.
Creating a Project for Spock

Part of tutorial

Creating a Project for Spock
Start by creating a new Java project which will be the basis for the rest of this tutorial.
Data Tables

Part of tutorial

Data Tables
Data tables offer the same functionality as data pipes, but this syntax can sometimes be more readable for more complex sets of data.
Given, When and Then

Part of tutorial

Given, When and Then
See how Spock tests are usually structured.
Expecting Exceptions

Part of tutorial

Expecting Exceptions
Tests don't check just the happy paths, sometimes we want to say we're expecting a specific Exception.
Data Pipes

Part of tutorial

Data Pipes
Often we want to test the same set of criteria with different sets of data. Data pipes is one mechanism to do this.
Groovy Tips for Java Developers

Part of tutorial

Groovy Tips for Java Developers
Groovy's syntax and functionality might be unusual for Java developers to begin with, but it can be very helpful for testing.
Helper Methods

Part of tutorial

Helper Methods
Sometimes we need to move test code into a separate method. In this section, we look at some tips for this.
Introduction

Part of tutorial

Introduction
Introduction to the tutorial and links to further reading.
Label Flexibility

Part of tutorial

Label Flexibility
Spock supports a range of different test labels to help you write the most readable tests.
Mocks

Part of tutorial

Mocks
We can provide "empty" objects, so we don't have to initialise the whole application to test a section, and we can use mocks to check our code is making the calls we expect.
Setting up the Dependencies

Part of tutorial

Setting up the Dependencies
Add Spock framework dependencies to the project.
Setup and Teardown

Part of tutorial

Setup and Teardown
See how to set up, or clean up, test classes and methods in Spock.
Shortcuts used

Part of tutorial

Shortcuts used
Finally, let's summarise the shortcuts we've learnt and see links to further information.
Specifications as Documentation

Part of tutorial

Specifications as Documentation
Spock tests don't just tell the computer how to test the application, they can help developers understand what the application is supposed to do.
Spock IntelliJ IDEA Plugin

Part of tutorial

Spock IntelliJ IDEA Plugin
Add the Spock plugin to IntelliJ IDEA to get some extra help in the IDE.
With

Part of tutorial

With
Sometimes we want to check more than one value on an object. We can use `with` to do this.
Stubs

Part of tutorial

Stubs
Stub can provide simple interfaces, so we don't have to initialise the whole application to test a section.
Verify All

Part of tutorial

Verify All
Test frameworks often stop the test at the first failure. Find out how to run all conditions to see which pass and which fail.
Profile your Application

Tip

Profile your Application
See how your application is performing with the built-in profiler.
Run your code with Docker, SSH or WSL

Tip

Run your code with Docker, SSH or WSL
Set a Run Target to change where your code runs.
Tips for Working with JetBrains Space

Tip

Tips for Working with JetBrains Space
Space is an all-in-one collaboration tool, including chat, code repository, code review, issue tracking, and much more.
Code Reviews with Space

Tip

Code Reviews with Space
Perform code reviews with Space without leaving the IDE.
Generate UML Diagrams for your Kotlin code

Tip

Generate UML Diagrams for your Kotlin code
JVM developers working with Kotlin code can create UML diagrams for their Kotlin classes, just like they could with Java classes.
Prevent a Package Depending Upon Another

Tip

Prevent a Package Depending Upon Another
Enforce architecture rules, such as data access layers not depending upon UI layers.
Lightweight Inspection Profile on Commit

Tip

Lightweight Inspection Profile on Commit
Fully customise the checks you must perform before you commit your changes to version control.
Create a Correct Child Sealed Class

Tip

Create a Correct Child Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to create child classes.
Create a Correct Parent Sealed Class

Tip

Create a Correct Parent Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to use them.
Preview HTML updates in Real Time

Tip

Preview HTML updates in Real Time
See what your changes look like as you make them to minimise errors.
Turn on Preview Features to try new Java Features

Tip

Turn on Preview Features to try new Java Features
The latest versions of Java contain "Preview Features", features that are fully functional but require developer feedback. Try them out in IntelliJ IDEA.
Convert from Record

Tip

Convert from Record
Use quick intentions to convert a record to a class.
Create Record

Tip

Create Record
Let IntelliJ IDEA support you in creating a new Java record class.
Identify and Fix Deprecated Calls

Tip

Identify and Fix Deprecated Calls
IntelliJ IDEA will warn you if you're using deprecated methods, and they will be shown with a strikethrough if they're marked for removal.
Use Pattern Matching for instanceof

Tip

Use Pattern Matching for instanceof
Inspections can guide us to use newer Java features.
Trisha Gee

Author

Trisha Gee
Java Champion