Preface

Modern software is driven by users wanting new features, with no defects, released rapidly – a very challenging task. Solo developers have given way to development teams working together on a single software product. Features are added in short iterative cycles, then released to production frequently – sometimes daily.

Achieving this requires excellence in development. We must ensure that our software is always ready to be deployed and free of defects when it is released into production. It must be easy for our developer colleagues to work with. Code must be easy for anyone to understand and change. When the team makes those changes, we must have confidence that our new features work properly and that we have not broken any existing features.

This book brings together proven techniques that help make this a reality.

Test-driven development (TDD), the SOLID principles, and hexagonal architecture enable developers to engineer code that is known to work and known to be easy to work with. Development focuses on the fundamentals of software engineering. These practices are the technical foundation behind a code base that is easy and safe to change and always ready to be deployed.

This book will enable you to write well-engineered, well-tested code. You will have confidence that your code works as you think it should. You will have the safety of an ever-growing suite of fast-running tests, keeping a watchful eye over the whole code base as the team makes changes. You will learn how to organize your code to avoid difficulties caused by external systems such as payment services or database engines. You will reduce your dependence on slower forms of testing.

You will be writing higher quality code, suitable for a Continuous Delivery approach.

Modern software requires a modern development approach. By the end of this book, you will have mastered the techniques to apply one.

Who this book is for

This book is primarily aimed at developers who are familiar with the Java language basics, and who want to be effective in a high-performance Agile development team. The techniques described in this book enable your code to be delivered to production with few defects, and a structure that can be easily and safely changed. This is the technical basis of agility.

The book’s early chapters will also be useful to business leaders who want to understand the costs and benefits of these approaches before committing to them.

What this book covers

Chapter 1, Building the Case for TDD, provides an understanding of the benefits TDD brings and how we got here.

Chapter 2, Using TDD to Create Good Code, covers some general good practices that will help us create well-engineered code as we apply TDD.

Chapter 3, Dispelling Common Myths about TDD, is a review of the common objections to using TDD that we might encounter, with suggestions for overcoming them. This chapter is suitable for business leaders who may have reservations about introducing a new technique into the development process.

Chapter 4, Building an Application Using TDD, involves setting up our development environment to build the Wordz application using TDD. It reviews how to work in short iterations using user stories.

Chapter 5, Writing Our First Test, introduces the basics of TDD with the Arrange, Act and Assert template. Writing the first test and production code for Wordz, we will look in detail at how TDD promotes a design step before we write code. We will consider various options and trade-offs, and then capture these decisions in a test.

Chapter 6, Following the Rhythms of TDD, demonstrates the red, green, refactor cycle as a rhythm of development. We decide on the next test to write, watch it fail, make it pass, and then refine our code to be safe and simple for the team to work with in the future.

Chapter 7, Driving Design – TDD and SOLID, builds on previous chapters showing how TDD provides rapid feedback on our design decisions by bringing SOLID into the fold. The SOLID principles are a useful set of guidelines to help design object-oriented code. This chapter is a review of those principles so that they are ready for us to apply in the rest of the book.

Chapter 8, Test Doubles – Stubs and Mocks, explains two critical techniques that allow us to swap out things that are difficult to test for things that are easier to test. By doing so, we can bring more of our code under a TDD unit test, reducing our need for slower integration testing.

Chapter 9, Hexagonal Architecture – Decoupling External Systems, presents a powerful design technique that enables us to fully decouple external systems such as databases and web servers from our core logic. We will introduce the concepts of ports and adapters here. This simplifies the use of TDD and as a benefit, provides resilience to any changes imposed on us by external factors.

Chapter 10, FIRST Tests and the Test Pyramid, outlines the test pyramid as a means of thinking about the different kinds of tests needed to fully test a software system. We discuss unit, integration, and end-to-end tests and the trade-offs between each type.

Chapter 11, How TDD Fits into Quality Assurance, explores how when using advanced test automation as described in this book, our QA engineers are freed up from some of the laborious detailed testing that they might have otherwise had to do. This chapter looks at how testing is now a whole-team effort throughout development, and how we can best combine our skills.

Chapter 12, Test First, Test Later, Test Never, reviews some different approaches to testing based on when we write the tests and what exactly we are testing. This will help us increase the quality of the tests we produce when applying TDD.

Chapter 13, Driving the Domain Layer, walks through applying TDD, SOLID, the test pyramid, and hexagonal architecture to the domain layer code of Wordz. Combined, these techniques enable us to bring most of the game logic under fast unit tests.

Chapter 14, Driving the Database Layer, offers guidance on writing the adapter code that connects to our SQL database, Postgres, now that we have decoupled the database code from the domain layer. We do this test-first, writing an integration test using the Database Rider test framework. The data access code is implemented using the JDBI library.

Chapter 15, Driving the Web Layer, rounds off as the final chapter of the book by explaining how to write an HTTP REST API that allows our Wordz game to be accessed as a web service. This is done test-first, using an integration test written using the tools built into the Molecule HTTP server library. Having completed this step, we will finally wire up the entire microservice ready to be run as a whole.

To get the most out of this book

This book assumes you know basic modern Java and can write short programs using classes, JDK 8 lambda expressions, and use the JDK 11 var keyword. It also assumes that you can use basic git commands, install software from web downloads onto your computer, and have basic familiarity with the IntelliJ IDEA Java IDE. Basic knowledge of SQL, HTTP, and REST will be helpful in the final chapters.

Software/hardware covered in the book

Operating system requirements

Amazon Corretto JDK 17 LTS

Windows, macOS, or Linux

IntelliJ IDEA 2022.1.3 Community Edition

Windows, macOS, or Linux

JUnit 5

Windows, macOS, or Linux

AssertJ

Windows, macOS, or Linux

Mockito

Windows, macOS, or Linux

DBRider

Windows, macOS, or Linux

Postgres

Windows, macOS, or Linux

psql

Windows, macOS, or Linux

Molecule

Windows, macOS, or Linux

git

Windows, macOS, or Linux

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Test-Driven-Development-with-Java. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/kLcmS.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system.”

A block of code is set as follows:

public class DiceRoll {
    private final int NUMBER_OF_SIDES = 6;
    private final RandomGenerator rnd =
                       RandomGenerator.getDefault();

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class DiceRoll {
    private final int NUMBER_OF_SIDES = 6;
    private final RandomGenerator rnd =
                       RandomGenerator.getDefault();

Any command-line input or output is written as follows:

private final int NUMBER_OF_SIDES = 6

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Select System info from the Administration panel.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Test-Driven Development with Java, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/978-1-80323-623-0

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.7