This GIT repository contains all source code for the book Java Testing With Spock published by Manning. Each chapter is offered as an independent Maven project.
You can get an overview of Spock and read a brief discussion on its features in the first chapter of the book. This chapter is freely available in PDF format.
To use the code samples you need the following:
- Java Development Kit version 7+
- Maven installed (mvn should be available in the command line)
- Internet access (for fetching library dependencies)
You can checkout/run any chapter on its own. You can either work directly on Github or download the code as a ZIP file For each chapter entering the command mvn test will compile and run all JUnit/Spock tests. You can also import a chapter (as a Maven project) in your favourite IDE. To gain syntax highlighting you also need to install Groovy support in your IDE.
- Listing 1.1 Sample Spock test FIXME
- Listing 1.2 Java class under test
- Listing 1.2 JUnit test
- Listing 1.3 Spock test for the Adder Java class
- Listing 1.4 JUnit test for two Java classes
- Listing 1.5 Spock test for two Java classes
- Listing 1.6 Introducing an artificial bug in the Java class under test
- Listing 1.7 A JUnit test where method names are unrelated to business value
- Listing 1.8 A Spock test where methods explain the business requirements
###Chapter 2
- Listing 2.1 Groovy class conventions
- Listing 2.1 Groovy class conventions
- Listing 2.2 Groovy field conventions
- Listing 2.3 A complete Groovy script
- Listing 2.4 A Spock test using concise Groovy code
- Listing 2.5 Creating and using a Java class from Groovy
- Listing 2.5 Creating and using a Java class from Groovy
- Listing 2.6 Groovy optional typing in variables
- Listing 2.7 Groovy optional typing in methods
- Listing 2.8 Using dynamic typing in Spock methods
- Listing 2.9 Groovy can convert everything to a boolean
- Listing 2.10 Groovy truth used in Spock tests
- Listing 2.11 JUnit test with multiple object creation statements
- Listing 2.12 Spock test with map-based constructors
- Listing 2.13 Groovy versus Java maps
- Listing 2.14 Groovy maps with non-scalar keys and values
- Listing 2.15 Groovy versus Java lists
- Listing 2.16 Creating Groovy lists and maps in test code
- Listing 2.17 Using Groovy lists
- Listing 2.18 Using Groovy maps
- Listing 2.19 Using Groovy String
- Listing 2.20 Using Groovy multiline strings
- Listing 2.21 Reading test data from a file in a Spock test
- Listing 2.22 Reading XML in Groovy
- Listing 2.23 Reading JSON in Groovy
- Listing 2.24 Groovy closures
- Listing 2.25 Using Groovy closures in Spock tests
- Listing 2.26 Domain classes in Java
- Listing 2.27 Using a Groovy builder for quick object creation
- Bonus listing - Complex object creation in Java
- Listing 2.28 Using Expandos to mock interfaces
- Listing 2.29 Using a Groovy Expando as test data generator
###Chapter 3
- Listing 3.1 A fire control system in Java
- Listing 3.1 A fire control system in Java
- Listing 3.2 A JUnit test for the fire control system
- Listing 3.3 JUnit test with complex structure (real example)
- Listing 3.4 JUnit test that tests two things - DO NOT DO THIS
- Listing 3.5 The full Spock test for the fire control system
- Listing 3.6 Testing the nuclear reactor scenarios with JUnit
- Listing 3.7 Testing the nuclear reactor scenarios with Spock
- Listing 3.8 Java classes for the temperature monitor and reader
- Listing 3.9 Stubbing with Spock
- Listing 3.10 Java classes for the temperature monitor, reader and reactor control
- Listing 3.11 Mocking and Stubbing with Spock
- Listing 3.12 Mocking and Stubbing with JUnit and Mockito
- Listing 3.13 Mocking/Stubbing in a Spock parameterized test
If you find a problem with the code feel free to open an issue. If you feel like contributing you can even create a pull request!
If you have comments and suggestions for the book please use the Manning forum. I monitor it regularly and it is helpful if other readers can see and respond to your comments.
Enjoy!