Table of Contents
Testing with JUnit
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started
Why you should busy yourself with unit tests
Reducing the defect rate
Improving the code quality
Increasing the development pace
Enhancing the specification density
Boosting confidence and courage
Setting the table
Choosing the ingredients
Organizing your code
Serving the starter
Introducing the example app
Understanding the nature of a unit test
Writing the first test
Evaluating test results
Writing tests first
Summary
2. Writing Well-structured Tests
The four phases of a unit test
Using a common test structure
Setting up the fixture
What goes up must come down
Verification
Choosing the next functionality to test
Start with the happy path
Conclude with corner cases
After the war
Getting the test names right
Test prefix
Behavior-expressing patterns
Reducing names to the essentials
Summary
3. Developing Independently Testable Units
Decomposing requirements
Separating concerns
Component dependencies
Understanding isolation
Delegating responsibilities to DOCs
Indirect input and output
Unit isolation with test doubles
Working with test doubles
Placeholder dummies
Fake it till you make it
Providing indirect input with stubs
Recording interactions with spies
Verifying behavior with mocks
Increasing efficiency with mock frameworks
The promised land?
Basic stubbing
Indirect output verification
Using test helpers
Motivation
The test helper class
The test helper object
Summary
4. Testing Exceptional Flow
Testing patterns
Using the fail statement
Annotated expectations
Verification with the ExpectedException rule
Capturing exceptions with closures
Treating collaborators
Fail fast
The stubbing of exceptional behavior
Summary
5. Using Runners for Particular Testing Purposes
Understanding the architecture
What are runners good for?
Looking at the big picture
Writing your own extension
Using custom runners
Furnishing a suite with test cases
Structuring suites into categories
Populating suites automatically
How about creating test doubles with annotations?
Writing dataset tests
Using parameterized tests
Reducing glue code with JUnitParams
Increasing the expressiveness of test descriptions with Burst
Summary
6. Reducing Boilerplate with JUnit Rules
Understanding rules
What are JUnit rules?
Writing your own rule
Configuring the fixture with annotations
Working with advanced concepts
Using ClassRules
The ordering of rule execution
Employing custom solutions
Working with system settings
Ignoring tests conditionally
Summary
7. Improving Readability with Custom Assertions
Working with the JUnit built-in assert approach
Understanding the basics
Reviewing the file session storage
Verifying the storage behavior
Improving readability with assertion helpers
Creating flexible expressions of intent with Hamcrest
Using matcher expressions
Writing custom matchers
Writing fluently readable assertions with AssertJ
Employing assertion chains
Creating your own asserts
Summary
8. Running Tests Automatically within a CI Build
Wrapping up the sample application
Refining the architecture
Separating concerns into modules
Setting up an automated CI build
What is continuous integration?
Principles and practices
Creating your own build
Integrating code coverage reports
Enlarging on code coverage
Automating reporting
Summary
A. References
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Index
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2015
Production reference: 1240815
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-660-3
www.packtpub.com
Author
Frank Appel
Reviewers
Stefan Birkner
Jose Muanis Castro
John Piasetzki
Acquisition Editor
Sonali Vernekar
Content Development Editor
Merwyn D'souza
Technical Editor
Humera Shaikh
Copy Editors
Sarang Chari
Sonia Mathur
Project Coordinator
Nikhil Nair
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Jason Monteiro
Production Coordinator
Nilesh R. Mohite
Cover Work
Nilesh R. Mohite
Frank Appel is a stalwart of agile methods and test-driven development in particular. He has over 2 decades of experience as a freelancer and understands software development as a type of craftsmanship. Having adopted the test first approach over a decade ago, he applies unit testing to all kinds of Java-based systems and arbitrary team constellations. He serves as a mentor, provides training, and blogs about these topics at codeaffine.com.
Stefan Birkner has a passion for software development. He has a strong preference for beautiful code, tests, and deployment automation. Stefan is a contributor to JUnit and maintains a few other libraries.
Jose Muanis Castro holds a degree in information systems. Originally from the sunny Rio de Janeiro, he now lives in Brooklyn with his wife and kids. At The New York Times, he works with recommendation systems on the personalization team. Previously, he worked on CMS and publishing platforms at Globo.com in Brazil.
Jose is a seasoned engineer with hands-on experience in several languages. He's passionate about continuous improvement, agile methods, and lean processes. With a lot of experience in automation, from testing to deploying, he constantly switches hats between development and operations. When he's not coding, he enjoys riding around on his bike. He was a reviewer on the 2014 book, Mastering Unit Testing Using Mockito and JUnit, Packt Publishing. His Twitter handle is @muanis
.
John Piasetzki has over 15 years of professional experience as a software developer. He started out doing programming jobs when he was young and obtained a bachelor of science degree in computer engineering. John was fortunate enough to get his start in programming by contributing to WordPress. He continued by working at IBM on WebSphere while getting his degree. Since then, he has moved on to smaller projects. John has worked with technologies such as Python, Ruby, and most recently, AngularJS. He's currently working as a software develope...