Java Testing with Spock - Konstantinos Kapelonis

Java Testing with Spock

Buch | Softcover
304 Seiten
2016
Manning Publications (Verlag)
978-1-61729-253-8 (ISBN)
43,75 inkl. MwSt
Learn testing with Spock from the ground up
Uses complex, yet understandable, semi-real test scenarios
Makes parameterized tests fun again
Written for Java developers. Experience with JUnit is helpful but not required.
While Java has changed a lot recently, the tools and techniques most Java developers use to test code are starting to show their age.

Spock is a modern testing framework that combines the features of JUnit, Mockito, and JBehave into a single powerful testing library. With Spock, developers can use Groovy to write more readable and concise tests, and built-in mocking means they no longer need an external framework.

Spock enables seamless integration testing and, with the intuitive Geb library, users can even handle functional testing of web applications.

Java Testing with Spock shows how to use Spock for a wide range of testing use cases in Java. It starts with a quick overview of Spock, and works through writing unit tests using the Groovy language, and discusses the best practices for test design as readers explore the Spock framework.

Along the way, readers will learn to write mocks, implement integration tests, use Spock's built-in BDD testing tools, and do functional web testing using Geb.

Readers new to Groovy will appreciate the succinct language tutorial that'll give just enough Groovy to use Spock effectively.

Konstantinos Kapelonis is a software engineer with 10+ years of programming experience ranging from writing bare metal C for the PlayStation 3 to Scheme code that mimics human reasoning. He works daily with Java and has a soft spot for code quality and build pipelines.

foreword
preface
acknowledgments
about this book
author online
about the authors
about the cover illustration
Part 1 Foundations and brief tour of Spock
1. Introducing the Spock testing framework
1.1. What is Spock?
1.1.1. Mocking and stubbing
1.1.2. Spock's design features
1.1.3. Spock's coding features
1.2. The need for a testing framework
1.2.1. Spock as an enterprise-ready test framework
1.2.2. Common ways to handle enterprise complexity
1.3. Spock: the groovier testing framework
1.3.1. Asserts vs. Assertions
1.3.2. Agnostic testing of Java and Groovy
1.3.3. Taking advantage of Groovy tricks in Spock tests
1.4. Getting an overview of Spock's main features
1.4.1. Enterprise testing
1.4.2. Data-driven tests
1.4.3. Mocking and stubbing
1.5. A first look at Spock in action
1.5.1. A simple test with JUnit
1.5.2. A simple test with Spock
1.5.3. Inspecting failed tests with Spock
1.6. Spock's position in the Java ecosystem
1.6.1. Making Spock groovy
1.6.2. Adding Spock tests to existing projects that have JUnit tests
1.6.3. Spock adoption path in a Java project
1.7. Comparing Spock and JUnit
1.7.1. Writing concise code with Groovy syntax
1.7.2. Mocking and stubbing with no external library
1.7.3. Using English sentences in Spock tests and reports
1.8. Summary
2. Groovy knowledge for Spock testing
2.1. What you need to know about Groovy
2.1.1. Groovy as a companion to Java
2.1.2. Accessing Java classes in a Groovy script
2.1.3. Declaring variables and methods in Groovy
2.1.4. Writing less code with Groovy
2.2. Groovy Power assert as a replacement for JUnit asserts
2.2.1. Understanding how Groovy handles asserts
2.2.2. Using Groovy assertions in Spock tests
2.3. Groovy features useful to Spock tests
2.3.1. Using map-based constructors
2.3.2. Using maps and lists in Groovy
2.3.3. Interpolating text with Groovy strings
2.4. Reading a test dataset from an external source
2.4.1. Reading a text file
2.4.2. Reading an XML file
2.4.3. Reading a JSON file
2.5. Advanced Groovy features useful to testing
2.5.1. Using Groovy closures
2.5.2. Creating test input with ObjectGraphBuilders
2.5.3. Creating test input with Expando
2.6. Summary
3. A tour of Spock functionality
3.1. Introducing the behavior-testing paradigm
3.1.1. The setup-stimulate-assert structure of JUnit
3.1.2. The given-when-then flow of Spock
3.2. Handling tests with multiple input sets
3.2.1. Existing approaches to multiple test-input parameters
3.2.2. Tabular data input with Spock
3.3. Isolating the class under test
3.3.1. The case of mocking/stubbing
3.3.2. Stubbing fake objects with Spock
3.3.3. Examining interactions of mocked objects
3.3.4. Combining mocks and stubs in parameterized tests
Part 2: Structuring Spock tests
4. Writing unit tests with Spock
4.1. Understanding Spock from the ground up
4.1.1. A simple test scenario
4.1.2. The given: block
4.1.3. The setup: block
4.1.4. The when: block
4.1.5. The then: block
4.1.6. The and: block
4.1.7. The expect: block
4.1.8. The cleanup: block
4.2. Converting requirements to Spock tests
4.2.1. Explaining the feature examined in a Spock test
4.2.2. Marking the class under test inside a Spock test
4.2.3. Describing the Spock unit test as a whole
4.2.4. Revising our view of a Spock test
4.3. Exploring the lifecycle of a Spock test
4.3.1. Setup and cleanup of a feature
4.3.2. Setup and cleanup of a specification
4.3.3. Long-lived objects with the @Shared annotation
4.3.4. Use of the old() method
4.4. Writing readable Spock tests
4.4.1. Structuring Spock tests
4.4.2. Ensuring that Spock tests are self-documenting
4.4.3. Modifying failure output
4.4.4. Using Hamcrest matchers
4.4.5. Grouping test code further
4.5. Summary
5. Parameterized tests
5.1. Detecting the need for parameterized tests
5.1.1. What are parameterized tests?
5.2. Using the where: block
5.2.1. Using data tables in the where: block
5.2.2. Understanding limitations of data tables
5.2.3. Performing easy maintenance of data tables
5.2.4. Exploring the lifecycle of the where: block
5.2.5. Using the @Unroll annotation for reporting individual test runs
5.2.6. Documenting parameterized tests
5.2.7. Using expressions and statements in data tables
5.3. Using data pipes for calculating input/output
5.3.1. Dynamically generated parameters
5.3.2. Parameters that stay constant
5.3.3. Parameters that depend on other parameters
5.4. Using dedicated data generators
5.4.1. Writing a custom data generator
5.4.2. Using multivalued data iterators
5.5. Working with third-party data generators
5.6. Summary
6. Mocking and stubbing
6.1. Using fake collaborators
6.1.1. Using fake collaborators to isolate a class in unit tests
6.1.2. Faking classes in Spock: mocks and stubs
6.1.3. Knowing when to use mocks and stubs
6.1.4. Exploring a sample application for an electronic shop system
6.2. Controlling input to the class under test with stubs
6.2.1. Basic stubbing of return values
6.2.2. Matching arguments leniently when a stubbed method is called
6.2.3. Using sequential stubs with different responses for each method call
6.2.4. Throwing exceptions when a stubbed method is called
6.2.5. Using dynamic stubs that check arguments when responding
6.2.6. Returning stubs from the responses of other stubs
6.3. Mocks: verifying values returned from the class under test
6.3.1. All capabilities of stubs exist in mocks as well
6.3.2. Simple mocking—examining whether a method was called
6.3.3. Verifying order of interactions
6.3.4. Verifying number of method calls of the mocked class
6.3.5. Verifying noninteractions for multiple mocked classes
6.3.6. Verifying types of arguments when a mocked method is called
6.3.7. Verifying arguments of method calls from mocked
6.4. Putting it all together: credit card charging in two steps
6.5. Architecture considerations for effective mocking/stubbing
6.5.1. Designing testable code that allows painless mocking
6.5.2. Understanding lenient vs. strict mocks
6.6. Summary
Part 3 Spock in the Enterprise
7. Integration and functional testing with Spock
7.1. Unit tests vs. integration tests vs. functional tests
7.1.1. Characteristics of the test categories
7.1.2. The testing pyramid
7.1.3. Spock support for integration and functional testing
7.1.4. Source code organization of the examples
7.2. Integration testing with Spock
7.2.1. Testing a Spring application
7.2.2. Narrowing down the Spring context inside Spock tests
7.2.3. Directly accessing the database with Groovy SQL
7.2.4. Integration testing with other containers (Java EE and Guice)
7.3. Functional testing of REST services with Spock
7.3.1. Working with a simple REST service
7.3.2. Testing REST services by using Java libraries
7.3.3. Using the @Stepwise annotation to run tests in order
7.3.4. Testing REST services using Groovy RESTClient
7.4. Functional testing of web applications with Spock
7.4.1. Browser automation with Geb
7.4.2. The example web application
7.4.3. Spock and Geb: a match made in heaven
7.4.4. Using Geb to interact with a web page
7.5. Running Spock tests as part of a build process
7.5.1. Splitting unit, integration, and functional tests
7.5.2. Getting code coverage from Spock tests
7.6. Summary
8. Spock features for enterprise testing
8.1. Using additional Spock features for enterprise tests
8.1.1. Testing the (non)existence of exceptions: thrown() and notThrown()
8.1.2. Mapping Spock tests to your issue-tracking system: @Issue
8.1.3. Failing tests that don't finish on time: @Timeout
8.1.4. Ignoring certain Spock tests
8.1.5. Automatic cleaning of resources: @AutoCleanup
8.2. Handling large Spock tests
8.2.1. Using helper methods to improve code readability
8.2.2. Reusing assertions in the then: block
8.2.3. Reusing interactions in the then: block
8.3. Creating partial mocks with spies
8.3.1. A sample application with special requirements
8.3.2. Spies with Spock
8.3.3. The need for spies shows a problematic code base
8.4. Summary
Appendixes
Appendix A: Installing Spock
A.1. Optional Groovy installation
A.2. Choosing a Spock version
A.3. Master example for Maven, Ant, and Gradle
A.3.1. Spock with Maven
A.3.2. Spock with Gradle
A.3.3. Spock in an enterprise environment
A.4. Spock tests in your IDE
A.4.1. Spock in Eclipse
A.4.2. Spock in the IntelliJ IDE
A.4.3. Spock in NetBeans
A.5. How to use the source code of this book
A.6. How to use the chapter code in Eclipse
A.7. How to use the chapter
A.8. Other resources
Appendix B: External Spock extensions and related tools
B.1. Detailed Spock reporting
B.2. Gradle-style Spock reports
B.3. Spock Genesis
B.4. Spock-Arquillian test runner
B.5. Using PowerMock with Spock
B.6. Spock InjectMocks extension
B.7. Spock Retry extension
B.8. Spock dbUnit extension
B.9. Spock Android extension
B.10. Spock Gherkin extension
B.11. Spock support in Serenity
B.12. Spock support in Allure

Erscheint lt. Verlag 14.4.2016
Verlagsort New York
Sprache englisch
Maße 189 x 234 mm
Gewicht 524 g
Themenwelt Informatik Programmiersprachen / -werkzeuge Java
Informatik Software Entwicklung Objektorientierung
Informatik Software Entwicklung Qualität / Testen
Schlagworte BDD • Groovy (Programmiersprache) • Java • Softwareentwicklung • Spock • Testen
ISBN-10 1-61729-253-2 / 1617292532
ISBN-13 978-1-61729-253-8 / 9781617292538
Zustand Neuware
Haben Sie eine Frage zum Produkt?
Mehr entdecken
aus dem Bereich
mit über 150 Workouts in Java und Python

von Luigi Lo Iacono; Stephan Wiefling; Michael Schneider

Buch (2023)
Carl Hanser (Verlag)
29,99
Einführung, Ausbildung, Praxis

von Christian Ullenboom

Buch | Hardcover (2023)
Rheinwerk (Verlag)
49,90