Testing with JUnit
eBook - ePub

Testing with JUnit

Frank Appel

Share book
  1. 200 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Testing with JUnit

Frank Appel

Book details
Book preview
Table of contents
Citations

About This Book

Master high quality software development driven by unit tests

About This Book

  • Design and implement robust system components by means of the de facto unit testing standard in Java
  • Reduce defect rate and maintenance effort, plus simultaneously increase code quality and development pace
  • Follow a step-by-step tutorial imparting the essential techniques based on real-world scenarios and code walkthroughs

Who This Book Is For

No matter what your specific background as a Java developer, whether you're simply interested in building up a safety net to reduce regressions of your desktop application or in improving your server-side reliability based on robust and reusable components, unit testing is the way to go. This book provides you with a comprehensive but concise entrance advancing your knowledge step-wise to a professional level.

What You Will Learn

  • Organize your test infrastructure and resources reasonably
  • Understand and write well structured tests
  • Decompose your requirements into small and independently testable units
  • Increase your testing efficiency with on-the-fly generated stand-in components and deal with the particularities of exceptional flow
  • Employ runners to adjust to specific test demands
  • Use rules to increase testing safety and reduce boilerplate
  • Use third party supplements to improve the expressiveness of your verification statements

In Detail

JUnit has matured to become the most important tool when it comes to automated developer tests in Java. Supported by all IDEs and build systems, it empowers programmers to deliver software features reliably and efficiently. However, writing good unit tests is a skill that needs to be learned; otherwise it's all too easy to end up in gridlocked development due to messed up production and testing code. Acquiring the best practices for unit testing will help you to prevent such problems and lead your projects to success with respect to quality and costs.

This book explains JUnit concepts and best practices applied to the test first approach, a foundation for high quality Java components delivered in time and budget.

From the beginning you'll be guided continuously through a practically relevant example and pick up background knowledge and development techniques step by step. Starting with the basics of tests organization you'll soon comprehend the necessity of well structured tests and delve into the relationship of requirement decomposition and the many-faceted world of test double usage. In conjunction with third-party tools you'll be trained in writing your tests efficiently, adapt your test case environment to particular demands and increase the expressiveness of your verification statements. Finally, you'll experience continuous integration as the perfect complement to support short feedback cycles and quality related reports for your whole team.

The tutorial gives a profound entry point in the essentials of unit testing with JUnit and prepares you for test-related daily work challenges.

Style and approach

This is an intelligible tutorial based on an ongoing and non-trivial development example. Profound introductions of concepts and techniques are provided stepwise as the programming challenges evolve. This allows you to reproduce and practice the individual skills thoroughly.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Testing with JUnit an online PDF/ePUB?
Yes, you can access Testing with JUnit by Frank Appel in PDF and/or ePUB format, as well as other popular books in Computer Science & Application Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781782166603
Edition
1

Testing with JUnit


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

Testing with JUnit

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

Credits

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

About the Author

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.

About the Reviewers

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...

Table of contents