Leanpub Header

Skip to main content

Test-Driven Development, Build Automation, Continuous Integration

with Java, Eclipse and friends

Get started with Test-Driven Development (write tests before the code), Build Automation (make the overall process of compilation and testing automatic with Maven), and Continuous Integration (commit changes and a server will perform the whole build of your code). Using Java, Eclipse, and their ecosystems.

Minimum price

$11.99

$16.99

You pay

$16.99

Author earns

$13.59
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
185
Readers
About

About

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.

Share this book

Author

About the Author

Lorenzo Bettini

Lorenzo Bettini is an Associate Professor in Computer Science at the Dipartimento di Statistica, Informatica, Applicazioni "Giuseppe Parenti", Università di Firenze, Italy. Previously, he was a researcher in Computer Science at Dipartimento di Informatica, Università di Torino, Italy.

He has a Masters Degree summa cum laude in Computer Science (Università di Firenze) and a PhD in "Logics and Theoretical Computer Science" (Università di Siena).

His research interests cover design, theory, and the implementation of statically typed programming languages and Domain Specific Languages.

He is the project lead of the Eclipse projects EMF Parsley, https://www.eclipse.org/emf-parsley/, and Xsemantics, https://projects.eclipse.org/projects/modeling.xsemantics. He is also a committer of the Eclipse projects Xtext, https://www.eclipse.org/Xtext/, and SWTBot, https://www.eclipse.org/swtbot/.

He is the author of the two editions of the book "Implementing Domain-Specific Languages with Xtext and Xtend", published by Packt Publishing (2013 and 2016).

He is also the author of about 90 research papers published in international conferences and international journals.

Contents

Table of Contents

About the author

Introduction

  1. Conventions
  2. Code of examples
  3. Errata
  4. Install the JDK

Book structure

1.Testing

  1. 1.1Automated tests
  2. 1.2Advantages of automated tests
  3. 1.3Test-Driven Development (TDD)
  4. 1.3.1Behavioral-Driven Development (BDD)

2.Eclipse

  1. 2.1Eclipse workspace
  2. Resource in Eclipse
  3. 2.2Perspectives and views
  4. 2.3Projects
  5. 2.3.1Importing an existing project
  6. 2.3.2Creating a new Java project
  7. 2.3.3Open projects and closed projects
  8. 2.3.4Automatic building
  9. 2.4IDE tools
  10. 2.4.1Content Assist
  11. 2.4.2Quick Fix
  12. 2.4.3Quick Assist
  13. 2.4.4Editable proposals
  14. 2.4.5The Source menu
  15. 2.5Eclipse Preferences
  16. 2.6Find Actions
  17. 2.7Eclipse Shortcuts
  18. 2.7.1Quick Search
  19. 2.8Eclipse Run configurations
  20. 2.9Browsing code
  21. 2.9.1Outline
  22. 2.9.2Navigate
  23. Camel Case
  24. 2.9.3Local History
  25. 2.10Code Mining

3.JUnit

  1. 3.1Structure of a test
  2. Test fixture
  3. 3.2A first example
  4. Do you spot any problems in the above test?
  5. 3.2.1Exception testing
  6. 3.2.2Other tests
  7. Code injection for package-private
  8. 3.2.3Revising
  9. Have we now tested everything correctly?
  10. 3.3External collaborators (dependencies)
  11. Dependency Injection
  12. 3.3.1Alternative implementations
  13. 3.4Testing private methods?
  14. 3.5Keep your tests clean and readable
  15. 3.5.1Beware of code duplication removal in tests
  16. 3.6Other testing libraries
  17. 3.6.1Using Hamcrest matchers
  18. 3.6.2Using AssertJ
  19. 3.7Keeping test code separate from main code
  20. 3.7.1Export a runnable JAR
  21. 3.8JUnit 5

4.TDD

  1. 4.1Introduction to TDD
  2. 4.1.1The Red-Green-Refactor cycle
  3. 4.1.2The three laws of TDD
  4. 4.1.3Remove duplication
  5. 4.2The Transformation Priority Premise
  6. 4.2.1A first example of TDD
  7. 4.3Three strategies for the green state
  8. 4.3.1The factorial example with TDD
  9. Remove redundant tests
  10. This is not a formal proof
  11. 4.4Small or big steps?
  12. 4.5Tests and induction (part 1)
  13. 4.6TDD with JUnit 4 Parameterized Tests
  14. 4.7TDD with JUnit 5 Parameterized Tests
  15. 4.8Testing abstract classes?
  16. 4.9Writing a test that succeeds

5.Code coverage

  1. 5.1JaCoCo and EclEmma
  2. 5.2How code coverage can help
  3. 5.3Code coverage percentage

6.Mutation Testing

  1. 6.1The first example with PIT
  2. Mutation testing requires a green suite.
  3. Bugs are likely to live in untested code
  4. 6.2Enabling additional mutators
  5. 6.3Further experiments with PIT
  6. 6.3.1Tests and induction (part 2)
  7. 6.3.2Mutations and iterative solutions
  8. 6.3.3Mutation testing on the MyStringUtils example
  9. 6.3.4Mutation testing on the Bank example
  10. 6.4Narrowing mutations

7.Maven

  1. 7.1Introduction to Maven
  2. 7.2Maven installation
  3. 7.3Let’s get started with a Maven archetype
  4. 7.3.1Create a Maven project from the command line
  5. 7.3.2Import a Maven project in Eclipse
  6. 7.3.3Create a Maven project from Eclipse using an archetype
  7. 7.4Structure of a Maven project
  8. 7.5Java settings
  9. 7.6Dependencies
  10. 7.6.1Maven coordinates (GAV)
  11. 7.6.2Dependencies in the POM
  12. 7.6.3SNAPSHOT
  13. 7.6.4Version ranges
  14. 7.7Eclipse m2e
  15. Output folders in a Maven project
  16. 7.8Properties
  17. 7.9Resources
  18. 7.9.1Layout of an Eclipse project
  19. 7.9.2Create a Maven project from Eclipse without an archetype
  20. 7.10The Eclipse m2e pom.xml multi-tab editor
  21. 7.11The bank example as a Maven project
  22. 7.12Build with Maven
  23. 7.12.1Maven lifecycles
  24. 7.12.2Run Maven from the command line
  25. 7.12.3Run Maven goals
  26. 7.12.4Run Maven from Eclipse
  27. 7.12.5Offline mode
  28. 7.12.6Updating dependencies
  29. 7.13Maven packaging
  30. 7.13.1Packaging “jar”
  31. 7.13.2Packaging “pom”
  32. 7.14Parent and modules
  33. 7.14.1Let’s create a parent project and a module project
  34. 7.14.2Effective POM
  35. 7.14.3Project aggregation and project inheritance
  36. 7.14.4Build a single module
  37. 7.14.5Dependency management
  38. 7.14.6Bill of Material (BOM)
  39. 7.14.7How Eclipse resolves the projects in the workspace
  40. 7.15Configuring a Maven plugin
  41. 7.15.1Configuring the Maven compiler plugin
  42. 7.15.2Generate source and javadoc jars
  43. 7.15.3Configuring the generated jar
  44. 7.15.4Creating a FatJar
  45. 7.15.5Plugin management
  46. 7.15.6Configuring the PIT Maven plugin
  47. 7.15.7Configuring the JaCoCo Maven plugin
  48. 7.16Maven profiles
  49. 7.16.1Don’t abuse profiles
  50. 7.17The bank example as a multi-module project
  51. 7.17.1The bank BOM
  52. 7.17.2The bank parent POM
  53. 7.17.3The bankaccount project
  54. 7.17.4The bank project
  55. 7.17.5The bank report project
  56. 7.17.6The bank app project
  57. 7.17.7The aggregator project
  58. 7.18JUnit 5 and Maven
  59. 7.19Maven deploy
  60. 7.20Maven wrapper
  61. 7.21Beyond Java 8
  62. 7.22Reproducibility in the build

8.Mocking

  1. 8.1Testing state and testing interactions
  2. 8.2Mockito: an overview
  3. 8.2.1Creating a mock
  4. 8.2.2Method stubbing
  5. 8.2.3Interaction verification
  6. 8.3Mockito: a tutorial
  7. The Repository pattern
  8. VerificationCollector
  9. 8.3.1Spy
  10. 8.3.2Spying and stubbing
  11. 8.3.3Stubbing and exceptions
  12. 8.3.4Subsequent stubbing
  13. 8.3.5Argument matchers
  14. 8.4Alternative ways of initializing mocks and other elements
  15. 8.5Mockito BDD style
  16. 8.6Mockito and JUnit 5
  17. 8.7Stubbing with answers
  18. 8.7.1Another example: Transactions
  19. 8.8Fakes
  20. 8.9What to mock

9.Git

  1. 9.1Let’s start experimenting with Git
  2. 9.1.1Create a git repository
  3. 9.1.2Git configuration
  4. Dealing with line endings
  5. 9.1.3States of the working tree
  6. 9.1.4Perform a commit
  7. 9.1.5Commit history
  8. 9.1.6Ignore files and directories
  9. 9.1.7Branches
  10. 9.1.8Reset
  11. 9.1.9Cherry-pick
  12. 9.1.10Stash
  13. 9.2Remote repositories
  14. 9.2.1Bare repository
  15. 9.2.2Adding a remote repository
  16. 9.2.3Initialize an empty remote repository
  17. 9.2.4Cloning a remote repository
  18. 9.2.5Push and fetch
  19. Forcing a push
  20. 9.2.6Rebasing
  21. 9.2.7Pull requests
  22. 9.3EGit (Git in Eclipse)
  23. 9.3.1Add an Eclipse project to a new Git repository
  24. 9.3.2The Git Repositories view
  25. 9.3.3Importing an existing project
  26. 9.3.4Branch operations
  27. 9.3.5Cloning from Eclipse
  28. 9.4GitHub
  29. 9.4.1Create a repository on GitHub
  30. 9.4.2GitHub pull requests
  31. 9.4.3Contributing to other projects

10.Continuous Integration

  1. 10.1Our running example
  2. 10.2GitHub Actions
  3. 10.2.1Caching dependencies
  4. 10.3Build matrix
  5. 10.3.1Building using different JDKs
  6. 10.3.2Building using different operating systems
  7. 10.3.3Configuring the build matrix
  8. 10.4Building pull requests
  9. 10.4.1Multiple workflows
  10. 10.5Storing artifacts
  11. 10.6Code coverage in CI
  12. 10.7Code coverage with Coveralls
  13. 10.7.1Using Coveralls from our computer
  14. 10.7.2Using Coveralls from GitHub Actions
  15. 10.7.3Coverage threshold
  16. 10.8Badges
  17. 10.9Reproducibility in CI
  18. 10.10Let’s revise our build process
  19. 10.11Automated dependency updates with Dependabot
  20. 10.12Further steps

11.Docker

  1. 11.1Let’s get started with Docker
  2. 11.2Run a server in a container
  3. 11.3Dockerize a Java application
  4. 11.3.1The Java application to dockerize
  5. 11.3.2The Dockerfile
  6. 11.4Windows containers
  7. 11.5Build the Docker image from Maven
  8. 11.6Using Docker in GitHub Actions
  9. 11.7Docker networks
  10. 11.8Docker compose
  11. 11.8.1Control startup order of container
  12. 11.9Docker in Eclipse
  13. 11.10Push to DockerHub
  14. 11.11Reproducibility in Docker

12.Integration tests

  1. 12.1Our running example
  2. 12.2Unit tests with databases
  3. 12.2.1Mock the MongoClient?
  4. 12.2.2Use an in-memory database
  5. 12.3Integration tests
  6. 12.3.1Source folder for integration tests
  7. 12.3.2Integration tests with Docker and Testcontainers
  8. 12.3.3Running integration tests with Maven
  9. 12.3.4Integration tests with Docker and Maven
  10. 12.3.5Running integration tests in GitHub Actions
  11. 12.4Unit or integration tests?

13.UI tests

  1. 13.1The running example
  2. 13.2UI unit tests
  3. AssertJ Swing in Linux
  4. AssertJ Swing in macOS
  5. 13.2.1Testing the GUI controls
  6. Don’t interact with the computer
  7. 13.2.2Implementing the StudentView interface
  8. 13.2.3Unit tests for the UI frame’s logic
  9. 13.3Running UI tests in GitHub Actions
  10. 13.4UI integration tests
  11. 13.5Multithreading
  12. 13.5.1Race conditions in the application
  13. 13.5.2Race conditions in the database
  14. 13.6Improvements

14.End-to-end tests

  1. 14.1The running example
  2. 14.2E2e tests for our application
  3. 14.3BDD with Cucumber
  4. 14.3.1Data tables
  5. 14.3.2Step decoupling
  6. 14.3.3High-level specifications
  7. 14.4Starting from the high-level specifications
  8. 14.5Change some low-level details

15.Code Quality

  1. 15.1Using SonarQube locally
  2. 15.2Analyze a project
  3. 15.2.1False positives and rule exclusion
  4. 15.2.2Analysis of test code
  5. 15.2.3Security hotspots
  6. 15.2.4Code coverage in SonarQube
  7. 15.3SonarCloud
  8. 15.4SonarLint (IDE integration)

16.Learning tests

  1. 16.1Dependency Injection with Google Guice
  2. 16.1.1Guice main concepts
  3. 16.1.2Singleton
  4. 16.1.3Field and method injection
  5. What to prefer
  6. 16.1.4Providers
  7. 16.1.5Binding annotations
  8. 16.1.6Overriding bindings
  9. 16.1.7Factories and AssistedInject
  10. 16.1.8Cyclic dependencies
  11. 16.1.9Provides
  12. 16.2Apply our learnings

Bibliography

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub