ebook img

Test-Driven Development with Java: Create higher-quality software by writing tests first with SOLID and hexagonal architecture PDF

348 Pages·2023·21.561 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Test-Driven Development with Java: Create higher-quality software by writing tests first with SOLID and hexagonal architecture

Test-Driven Development with Java Create higher-quality software by writing tests first with SOLID and hexagonal architecture Alan Mellor BIRMINGHAM—MUMBAI Test-Driven Development with Java Copyright © 2022 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(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Group Product Manager: Gebin George Publishing Product Manager: Arvind Sharma Senior Editor: Nisha Cleetus Technical Editor: Jubit Pincy Copy Editor: Safis Editing Project Coordinator: Manisha Singh Proofreader: Safis Editing Indexer: Subalakshmi Govindhan Production Designer: Shankar Kalbhor Business Development Executive: Kriti Sharma Marketing Coordinator: Sonakshi Bubbar First published: January 2023 Production reference: 1231222 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80323-623-0 www.packt.com In memory of my mum, Eva Mellor (1928 – 2022). You saw me start this book but not finish it. If I’m perfectly honest, you wouldn’t have enjoyed it as much as your Georgette Heyer novels. – Alan Mellor Contributors About the author Alan Mellor is an academy lead at BJSS, training the next generation of consulting software engineers, and the author of Java OOP Done Right: Create object oriented code you can be proud of with modern Java. Alan started with a Sinclair ZX81 computer with 1K of RAM and is very happy to have better computers now. Alan’s work includes industrial control in C, web applications for e-commerce, gaming and banking in Java and Go, and document warehousing in C++. His most visible code is part of Nokia Bounce and the RAF Red Arrows flight simulator if you go back far enough. I want to thank my wife Stephanie without whose support this book would not have been possible. I’m grateful to everyone who has taught me about software engineering, whether in person or via their books. All my love to Jake and Katy. You two are awesome. About the reviewers Jeff Langr has been building software professionally for over 4 decades. He’s recognized as the author of five books on software development, including Modern C++ Programming with Test–Driven Development: Code Better, Sleep Better, Agile in a Flash (with Tim Ottinger), and Agile in a Flash: Speed-learning Agile Software Development. Jeff is also a co-author of the best-selling book Clean Code. He’s written over a hundred published articles and several hundred blog posts on his site (https://langrsoft.com). Nikolai Avteniev started his professional career at JPMorgan Chase, participated in the Extreme Programming Pilot, and learned how to apply test-driven development and continuous integration. After graduating from NYU with a degree in computer science, he took the experience of building and running an Agile development team to Real Time Risk Systems as one of the founding engineers. Nikolai later joined New York City AdTech start-up Intent Media and then moved on to building software teams and systems at LinkedIn (https://engineering.linkedin.com/blog/2017/08/ getting-to-know-nikolai-avteniev). Additionally, Nikolai teaches software engineering at the City University of New York. Currently, he works at Stripe, helping grow the GDP of the internet safely. Table of Contents Preface xv Part 1: How We Got to TDD 1 Building the Case for TDD 3 Writing code badly 3 Coupling and cohesion 10 Understanding why bad code is written 5 Decreasing team performance 12 Recognizing bad code 6 Diminishing business outcomes 14 Bad variable names 6 Summary 16 Bad function, method, and class names 6 Questions and answers 16 Error-prone constructs 9 Further reading 16 2 Using TDD to Create Good Code 17 Designing good quality code 17 Preventing logic flaws 25 Say what you mean, mean what you say 18 Protecting against future defects 27 Take care of the details in private 19 Documenting our code 28 Avoid accidental complexity 20 Summary 29 Revealing design flaws 22 Questions and answers 29 Analyzing the benefits of writing tests before Further reading 30 production code 23 viii Table of Contents 3 Dispelling Common Myths about TDD 31 Writing tests slows me down 31 Managing your expectations of TDD 37 Understanding the benefits of slowing down 32 Our code is too complex to test 37 Overcoming objections to tests Understanding the causes of untestable code 37 slowing us down 33 Reframing the relationship between good Tests cannot prevent every bug 34 design and simple tests 38 Understanding why people say tests cannot Managing legacy code without tests 39 catch every bug 34 I don’t know what to test until I write Overcoming objections to not catching the code 39 every bug 35 Understanding the difficulty of starting with How do you know the tests are right? 35 testing 40 Understanding the concerns behind writing Overcoming the need to write production broken tests 35 code first 40 Providing reassurance that we test our tests 36 Summary 41 TDD guarantees good code 36 Questions and answers 41 Understanding problem-inflated expectations 36 Further reading 41 Part 2: TDD Techniques 4 Building an Application Using TDD 45 Technical requirements 45 Reading user stories – the building block of planning 51 Preparing our development environment 46 Combining agile development with TDD 52 Installing the IntelliJ IDE 46 Setting up the Java project and libraries 46 Summary 53 Introducing the Wordz application 48 Questions and answers 53 Describing the rules of Wordz 49 Further reading 54 Exploring agile methods 50 Table of Contents ix 5 Writing Our First Test 55 Technical requirements 55 Preserving encapsulation 64 Starting TDD: Arrange-Act-Assert 56 Learning from our tests 65 Defining the test structure 56 A messy Arrange step 65 Working backward from outcomes 58 A messy Act step 66 Increasing workflow efficiency 59 A messy Assert step 66 Defining a good test 59 Limitations of unit tests 66 Code coverage – an often-meaningless metric 67 Applying the FIRST principles 60 Writing the wrong tests 67 Using one assert per test 61 Deciding on the scope of a unit test 61 Beginning Wordz 67 Catching common errors 62 Summary 73 Asserting exceptions 63 Questions and answers 73 Only testing public methods 64 6 Following the Rhythms of TDD 75 Technical requirements 75 Writing our next tests for Wordz 79 Following the RGR cycle 75 Summary 92 Starting on red 76 Questions and answers 92 Keep it simple – moving to green 77 Further reading 93 Refactoring to clean code 78 7 Driving Design – TDD and SOLID 95 Technical requirements 96 Simplified future maintenance 100 Test guide – we drive the design 96 Counter-example – shapes code that violates SRP 101 SRP – simple building blocks 97 Applying SRP to simplify future maintenance 102 Too many responsibilities make code harder Organizing tests to have a single responsibility 104 to work with 99 Ability to reuse code 100 DIP – hiding irrelevant details 104

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.