E-Book, Englisch, 348 Seiten
Mellor Test-Driven Development with Java
1. Auflage 2024
ISBN: 978-1-80323-796-1
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection
Create higher-quality software by writing tests first with SOLID and hexagonal architecture
E-Book, Englisch, 348 Seiten
ISBN: 978-1-80323-796-1
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection
Test-driven development enables developers to craft well-designed code and prevent defects. It's a simple yet powerful tool that helps you focus on your code design, while automatically checking that your code works correctly. Mastering TDD will enable you to effectively utilize design patterns and become a proficient software architect.
The book begins by explaining the basics of good code and bad code, bursting common myths, and why Test-driven development is crucial. You'll then gradually move toward building a sample application using TDD, where you'll apply the two key rhythms -- red, green, refactor and arrange, act, assert. Next, you'll learn how to bring external systems such as databases under control by using dependency inversion and test doubles. As you advance, you'll delve into advanced design techniques such as SOLID patterns, refactoring, and hexagonal architecture. You'll also balance your use of fast, repeatable unit tests against integration tests using the test pyramid as a guide. The concluding chapters will show you how to implement TDD in real-world use cases and scenarios and develop a modern REST microservice backed by a Postgres database in Java 17.
By the end of this book, you'll be thinking differently about how you design code for simplicity and how correctness can be baked in as you go.
Autoren/Hrsg.
Fachgebiete
Weitere Infos & Material
Table of Contents - Building the Case for TDD
- Using TDD to Create Good Code
- Dispelling Common Myths about TDD
- Building a typical application using TDD
- Writing our First Test
- Following the Rhythms of TDD
- Driving Design: TDD and SOLID
- Test Doubles: Stubs and Mocks
- Hexagonal Architecture: Decoupling external systems
- FIRST Tests and the Test Pyramid
- Exploring TDD with Quality Assurance
- Test First, Test Later, Test Never
- Driving the domain layer
- Driving the database layer
- Driving the Web Layer
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
, , provides an understanding of the benefits TDD brings and how we got here.
, , covers some general good practices that will help us create well-engineered code as we apply 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.
, , involves setting up our development environment to build the Wordz application using TDD. It reviews how to work in short iterations using user stories.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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.
, , 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,... |




