Test-Driven Development, Build Automation, Continuous Integration
Test-Driven Development, Build Automation, Continuous Integration
with Java, Eclipse and friends
About the Book
The main subject of this book is software testing. The main premise is that testing is a crucial part of software development. You need to make sure that the software you write behaves correctly. You can manually test your software. However, manual tests require lots of manual work and it is error-prone.
On the contrary, this book focuses on automated tests, which can be done at several levels. In the book, we will see a few types of tests, starting from those that test a single component in isolation to those that test the entire application. We will also deal with tests in the presence of a database and with tests that verify the correct behavior of the graphical user interface.
In particular, we will describe and apply the Test-Driven Development methodology, writing tests before the actual code.
Throughout the book, we will use Java as the main programming language. We use Eclipse as the IDE. Both Java and Eclipse have a huge ecosystem of "friends", that is, frameworks, tools, and plugins. Many of them are related to automated tests and perfectly fit the goals of the book. We will use JUnit throughout the book as the main Java testing framework.
it is also important to be able to completely automate the build process. In fact, another relevant subject of the book is Build Automation. We will use one of the mainstream tools for build automation in the Java world, Maven.
We will use Git as the Version Control System and GitHub as the hosting service for our Git repositories. We will then connect our code hosted on GitHub with a cloud platform for Continuous Integration. In particular, we will use GitHub Actions. With the Continuous Integration process, we will implement a workflow where each time we commit a change in our Git repository, the CI server will automatically run the automated build process, compiling all the code, running all the tests and possibly create additional reports concerning the quality of our code and of our tests.
The code quality of tests can be measured in terms of a few metrics using code coverage and mutation testing. Other metrics are based on static analysis mechanisms, inspecting the code in search of bugs, code smells, and vulnerabilities. For such a static analysis we will use SonarQube and its free cloud version SonarCloud.
When we need our application to connect to a service like a database, we will use Docker a virtualization program, based on containers, that is much more lightweight than standard virtual machines. Docker will allow us to configure the needed services in advance, once and for all, so that the services running in the containers will take part in the reproducibility of the whole build infrastructure. The same configuration of the services will be used in our development environment, during build automation, and in the CI server.
Most of the chapters have a "tutorial" nature. Besides a few general explanations of the main concepts, the chapters will show lots of code. It should be straightforward to follow the chapters and write the code to reproduce the examples. All the sources of the examples are available on GitHub.
The main goal of the book is to give the basic concepts of the techniques and tools for testing, build automation, and continuous integration. Of course, the descriptions of these concepts you find in this book are far from being exhaustive. However, you should get enough information to get started with all the presented techniques and tools.
Table of Contents
- About the author
-
Introduction
- Conventions
- Code of examples
- Errata
- Install the JDK
- Book structure
-
1. Testing
- 1.1 Automated tests
- 1.2 Advantages of automated tests
-
1.3 Test-Driven Development (TDD)
- 1.3.1 Behavioral-Driven Development (BDD)
-
2. Eclipse
- 2.1 Eclipse workspace
- 2.2 Perspectives and views
-
2.3 Projects
- 2.3.1 Importing an existing project
- 2.3.2 Creating a new Java project
- 2.3.3 Open projects and closed projects
- 2.3.4 Automatic building
-
2.4 IDE tools
- 2.4.1 Content Assist
- 2.4.2 Quick Fix
- 2.4.3 Quick Assist
- 2.4.4 Editable proposals
- 2.4.5 The Source menu
- 2.5 Eclipse Preferences
- 2.6 Find Actions
-
2.7 Eclipse Shortcuts
- 2.7.1 Quick Search
- 2.8 Eclipse Run configurations
-
2.9 Browsing code
- 2.9.1 Outline
- 2.9.2 Navigate
- 2.9.3 Local History
- 2.10 Code Mining
-
3. JUnit
- 3.1 Structure of a test
-
3.2 A first example
- 3.2.1 Exception testing
- 3.2.2 Other tests
- 3.2.3 Revising
-
3.3 External collaborators (dependencies)
- 3.3.1 Alternative implementations
- 3.4 Testing private methods?
-
3.5 Keep your tests clean and readable
- 3.5.1 Beware of code duplication removal in tests
-
3.6 Other testing libraries
- 3.6.1 Using Hamcrest matchers
- 3.6.2 Using AssertJ
-
3.7 Keeping test code separate from main code
- 3.7.1 Export a runnable JAR
- 3.8 JUnit 5
-
4. TDD
-
4.1 Introduction to TDD
- 4.1.1 The Red-Green-Refactor cycle
- 4.1.2 The three laws of TDD
- 4.1.3 Remove duplication
-
4.2 The Transformation Priority Premise
- 4.2.1 A first example of TDD
-
4.3 Three strategies for the green state
- 4.3.1 The factorial example with TDD
- 4.4 Small or big steps?
- 4.5 Tests and induction (part 1)
- 4.6 TDD with JUnit 4 Parameterized Tests
- 4.7 TDD with JUnit 5 Parameterized Tests
- 4.8 Testing abstract classes?
- 4.9 Writing a test that succeeds
-
4.1 Introduction to TDD
-
5. Code coverage
- 5.1 JaCoCo and EclEmma
- 5.2 How code coverage can help
- 5.3 Code coverage percentage
-
6. Mutation Testing
- 6.1 The first example with PIT
- 6.2 Enabling additional mutators
-
6.3 Further experiments with PIT
- 6.3.1 Tests and induction (part 2)
- 6.3.2 Mutations and iterative solutions
- 6.3.3 Mutation testing on the MyStringUtils example
- 6.3.4 Mutation testing on the Bank example
- 6.4 Narrowing mutations
-
7. Maven
- 7.1 Introduction to Maven
- 7.2 Maven installation
-
7.3 Let’s get started with a Maven archetype
- 7.3.1 Create a Maven project from the command line
- 7.3.2 Import a Maven project in Eclipse
- 7.3.3 Create a Maven project from Eclipse using an archetype
- 7.4 Structure of a Maven project
- 7.5 Java settings
-
7.6 Dependencies
- 7.6.1 Maven coordinates (GAV)
- 7.6.2 Dependencies in the POM
- 7.6.3 SNAPSHOT
- 7.6.4 Version ranges
- 7.7 Eclipse m2e
- 7.8 Properties
-
7.9 Resources
- 7.9.1 Layout of an Eclipse project
- 7.9.2 Create a Maven project from Eclipse without an archetype
- 7.10 The Eclipse m2e pom.xml multi-tab editor
- 7.11 The bank example as a Maven project
-
7.12 Build with Maven
- 7.12.1 Maven lifecycles
- 7.12.2 Run Maven from the command line
- 7.12.3 Run Maven goals
- 7.12.4 Run Maven from Eclipse
- 7.12.5 Offline mode
- 7.12.6 Updating dependencies
-
7.13 Maven packaging
- 7.13.1 Packaging “jar”
- 7.13.2 Packaging “pom”
-
7.14 Parent and modules
- 7.14.1 Let’s create a parent project and a module project
- 7.14.2 Effective POM
- 7.14.3 Project aggregation and project inheritance
- 7.14.4 Build a single module
- 7.14.5 Dependency management
- 7.14.6 Bill of Material (BOM)
- 7.14.7 How Eclipse resolves the projects in the workspace
-
7.15 Configuring a Maven plugin
- 7.15.1 Configuring the Maven compiler plugin
- 7.15.2 Generate source and javadoc jars
- 7.15.3 Configuring the generated jar
- 7.15.4 Creating a FatJar
- 7.15.5 Plugin management
- 7.15.6 Configuring the PIT Maven plugin
- 7.15.7 Configuring the JaCoCo Maven plugin
-
7.16 Maven profiles
- 7.16.1 Don’t abuse profiles
-
7.17 The bank example as a multi-module project
- 7.17.1 The bank BOM
- 7.17.2 The bank parent POM
- 7.17.3 The bankaccount project
- 7.17.4 The bank project
- 7.17.5 The bank report project
- 7.17.6 The bank app project
- 7.17.7 The aggregator project
- 7.18 JUnit 5 and Maven
- 7.19 Maven deploy
- 7.20 Maven wrapper
- 7.21 Beyond Java 8
- 7.22 Reproducibility in the build
-
8. Mocking
- 8.1 Testing state and testing interactions
-
8.2 Mockito: an overview
- 8.2.1 Creating a mock
- 8.2.2 Method stubbing
- 8.2.3 Interaction verification
-
8.3 Mockito: a tutorial
- 8.3.1 Spy
- 8.3.2 Spying and stubbing
- 8.3.3 Stubbing and exceptions
- 8.3.4 Subsequent stubbing
- 8.3.5 Argument matchers
- 8.4 Alternative ways of initializing mocks and other elements
- 8.5 Mockito BDD style
- 8.6 Mockito and JUnit 5
-
8.7 Stubbing with answers
- 8.7.1 Another example: Transactions
- 8.8 Fakes
- 8.9 What to mock
-
9. Git
-
9.1 Let’s start experimenting with Git
- 9.1.1 Create a git repository
- 9.1.2 Git configuration
- 9.1.3 States of the working tree
- 9.1.4 Perform a commit
- 9.1.5 Commit history
- 9.1.6 Ignore files and directories
- 9.1.7 Branches
- 9.1.8 Reset
- 9.1.9 Cherry-pick
- 9.1.10 Stash
-
9.2 Remote repositories
- 9.2.1 Bare repository
- 9.2.2 Adding a remote repository
- 9.2.3 Initialize an empty remote repository
- 9.2.4 Cloning a remote repository
- 9.2.5 Push and fetch
- 9.2.6 Rebasing
- 9.2.7 Pull requests
-
9.3 EGit (Git in Eclipse)
- 9.3.1 Add an Eclipse project to a new Git repository
- 9.3.2 The Git Repositories view
- 9.3.3 Importing an existing project
- 9.3.4 Branch operations
- 9.3.5 Cloning from Eclipse
-
9.4 GitHub
- 9.4.1 Create a repository on GitHub
- 9.4.2 GitHub pull requests
- 9.4.3 Contributing to other projects
-
9.1 Let’s start experimenting with Git
-
10. Continuous Integration
- 10.1 Our running example
-
10.2 GitHub Actions
- 10.2.1 Caching dependencies
-
10.3 Build matrix
- 10.3.1 Building using different JDKs
- 10.3.2 Building using different operating systems
- 10.3.3 Configuring the build matrix
-
10.4 Building pull requests
- 10.4.1 Multiple workflows
- 10.5 Storing artifacts
- 10.6 Code coverage in CI
-
10.7 Code coverage with Coveralls
- 10.7.1 Using Coveralls from our computer
- 10.7.2 Using Coveralls from GitHub Actions
- 10.7.3 Coverage threshold
- 10.8 Badges
- 10.9 Reproducibility in CI
- 10.10 Let’s revise our build process
- 10.11 Further steps
-
11. Docker
- 11.1 Let’s get started with Docker
- 11.2 Run a server in a container
-
11.3 Dockerize a Java application
- 11.3.1 The Java application to dockerize
- 11.3.2 The Dockerfile
- 11.4 Windows containers
- 11.5 Build the Docker image from Maven
- 11.6 Using Docker in GitHub Actions
- 11.7 Docker networks
-
11.8 Docker compose
- 11.8.1 Control startup order of container
- 11.9 Docker in Eclipse
- 11.10 Push to DockerHub
- 11.11 Reproducibility in Docker
-
12. Integration tests
- 12.1 Our running example
-
12.2 Unit tests with databases
- 12.2.1 Mock the MongoClient?
- 12.2.2 Use an in-memory database
-
12.3 Integration tests
- 12.3.1 Source folder for integration tests
- 12.3.2 Integration tests with Docker and Testcontainers
- 12.3.3 Running integration tests with Maven
- 12.3.4 Integration tests with Docker and Maven
- 12.3.5 Running integration tests in GitHub Actions
- 12.4 Unit or integration tests?
-
13. UI tests
- 13.1 The running example
-
13.2 UI unit tests
- 13.2.1 Testing the GUI controls
- 13.2.2 Implementing the StudentView interface
- 13.2.3 Unit tests for the UI frame’s logic
- 13.3 Running UI tests in GitHub Actions
- 13.4 UI integration tests
-
13.5 Multithreading
- 13.5.1 Race conditions in the application
- 13.5.2 Race conditions in the database
- 13.6 Improvements
-
14. End-to-end tests
- 14.1 The running example
- 14.2 E2e tests for our application
-
14.3 BDD with Cucumber
- 14.3.1 Data tables
- 14.3.2 Step decoupling
- 14.3.3 High-level specifications
- 14.4 Starting from the high-level specifications
- 14.5 Change some low-level details
-
15. Code Quality
- 15.1 Using SonarQube locally
-
15.2 Analyze a project
- 15.2.1 False positives and rule exclusion
- 15.2.2 Analysis of test code
- 15.2.3 Security hotspots
- 15.2.4 Code coverage in SonarQube
- 15.2.5 JUnit reports
- 15.3 SonarCloud
- 15.4 SonarLint (IDE integration)
-
16. Learning tests
-
16.1 Dependency Injection with Google Guice
- 16.1.1 Guice main concepts
- 16.1.2 Singleton
- 16.1.3 Field and method injection
- 16.1.4 Providers
- 16.1.5 Binding annotations
- 16.1.6 Overriding bindings
- 16.1.7 Factories and AssistedInject
- 16.1.8 Cyclic dependencies
- 16.1.9 Provides
- 16.2 Apply our learnings
-
16.1 Dependency Injection with Google Guice
- Bibliography
- Notes
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned$12,307,240writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Personal Finance
Jason AndersonThis textbook provides an in-depth analysis on personal finance that is both practical and straightforward in its approach. It has been written in such a way that the readers can gain knowledge without getting overwhelmed by the technical terms. Suitable for both beginners and advanced learners.
Getting to Know IntelliJ IDEA
Trisha Gee and Helen ScottIf we treat our IDE as a text editor, we are doing ourselves a disservice. Using a combination of tutorials and a questions-and-answers approach, Getting to Know IntelliJ IDEA will help you find ways to use IntelliJ IDEA that enable you to work comfortably and productively as a professional developer.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Machine Learning Q and AI
Sebastian Raschka, PhDHave you recently completed a machine learning or deep learning course and wondered what to learn next? With 30 questions and answers on key concepts in machine learning and AI, this book provides bite-sized bits of knowledge for your journey to becoming a machine learning expert.
Stats One
William FooteThe Rails 7 Way
Obie Fernandez, Lucas Dohmen, and Tom Henrik AadlandThe Rails™ 7 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. It illuminates the entire Rails 7 API, its most powerful idioms, design approaches, and libraries. Building on the previous editions, this edition has been heavily refactored and updated.
Gradual Modularization for Ruby and Rails
Stephan HagemannGet yourself a new tool to manage your Rails application and your growing engineering organization! Prevent the ball-of-mud (and fix it!). Go for microservices or SOA if it makes sense not just because you don't have any other tool. Do all this through a low-overhead tool: packages. Enable better conversations to make practical changes today.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
1500 QUIZ COMMENTATI (3 libri)
3 Books
Tre libri dei QUIZ MMG Commentati al prezzo di DUE! I QUIZ dei concorsi ufficiali di Medicina Generale relativi agli anni: 2000-2001-2003-2012-2013-2014-2015-2016-2017-2018-2019-2020-2021 +100 inediti Raccolti in unico bundle per aiutarvi nello studio e nella preparazione al concorso. All'interno di ogni libro i quiz sono stati suddivisi per... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #5
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #9
Development and Deployment of Multiplayer Online Games, Part ARCH. Architecture (Vol. I-III)
3 Books
What's the Big Idea? The idea behind this book is to summarize the body of knowledge that already exists on multiplayer games but is not available in one single place.And quite a fewof the issues discussed within this series (planned as three nine volumes ~300 pages each), while known in the industry, have not been published at all (except for... - #10
Growing Agile: The Complete Coach's Guide
7 Books
Growing Agile: Coach's Guide Series This bundle provides a collection of training and workshop plans for a variety of agile topics. The series is aimed at agile coaches, trainers and ScrumMasters who often find themselves needing to help teams understand agile concepts. Each book in the series provides the plans, slides, handouts and activity...