Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Test-Driven Java Development
Test-Driven Java Development

Test-Driven Java Development: Invoke TDD principles for end-to-end application development with Java

eBook
$27.98 $39.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Test-Driven Java Development

Chapter 2. Tools, Frameworks, and Environments

 

"We become what we behold. We shape our tools and then our tools shape us."

 
 --Marshall McLuhan

As every soldier knows his weapons, a programmer must be familiar with the development ecosystem and those tools that make programming much easier. Whether you are already using any of these tools at work or home, it is worth taking a look at many of them and comparing the features, advantages, and disadvantages. Let's do an overview of what we can find nowadays about the following topics and construct a small project to get familiar with some of them.

We won't go into the details of those tools and frameworks since that will be done later on in the following chapters. The goal is to get you up and running and provide you with a short overview of what they do and how.

The following topics will be covered in this chapter:

  • Git
  • Virtual machines
  • Build tools
  • The integrated development environment
  • Unit testing frameworks...

Git

Git is the most popular revision control system. For that reason, all the code used in this book is stored in Bitbucket (https://bitbucket.org/). If you don't have it already, install Git. Distributions for all the popular operating systems can be found at http://git-scm.com.

Many graphical interfaces are available for Git; some of them being Tortoise (https://code.google.com/p/tortoisegit), Source Tree (https://www.sourcetreeapp.com), and Tower (http://www.git-tower.com/).

Virtual machines

Even though they are outside the topic of this book, virtual machines are a powerful tool and a first-class citizen in a good development environment. They provide dynamic and easy-to-use resources in isolated systems so they can be used and dropped at the time we need them. This helps developers to focus on their tasks instead of wasting their time creating or installing required services from scratch. This is the reason why virtual machines have found room in here. We want to take advantage of them to keep you focused on the code.

In order to have the same environment no matter the OS you're using, we'll be creating virtual machines with Vagrant and deploying required applications with Docker. We chose Ubuntu as a base operating system in our examples, just because it is a popular, commonly used Unix-like distribution. Most of these technologies are platform-independent, but occasionally you won't be able to follow the instructions found here because you...

Build tools

With time, code tends to grow both in complexity and size. This occurs in the software industry by its nature. All products evolve constantly and new requirements are made and implemented across a product's life. Build tools offer a way to make managing project life cycle's as straightforward as possible, by following a few code conventions such as the organization of your code in a specific way and usage of naming a convention for your classes or a determined project structure formed by different folders and files.

Some of you might be familiar with Maven or Ant. They are a great couple of Swiss army knives for handling projects, but we are here to learn so we decided to use Gradle. Some of the advantages of Gradle are reduced boiler plate code, resulting in a much shortdr file and a more readable configuration file. Among others, Google uses it as its build tool. It is supported by IntelliJ IDEA and is quite easy to learn and work with. Most of the functionalities...

The integrated development environment

As many tools and technologies will be covered, we recommend using IntelliJ IDEA as the tool for code development. The main reason is that this IDE works without any tedious configuration. The Community Edition (IntelliJ IDEA CE) comes with a bunch of built-in features and plugins that make coding easy and efficient. It automatically recommends plugins that can be installed depending on the file extension. As IntelliJ IDEA is the choice we made for this book, you will find references and steps referring to its actions or menus. Readers should find a proper way to emulate those steps if they are using other IDEs. Refer to https://www.jetbrains.com/idea/ for instructions on how to download and install IntelliJ IDEA.

The IDEA demo project

Let's create the base layout of the demo project. This project will be throughout along this chapter to illustrate all the topics that are covered. Java will be the programming language and Gradle (http://gradle.org...

Unit testing frameworks

In this section, two of the most used Java frameworks for unit testing are shown and briefly commented on. We will focus on their syntax and main features by comparing a test class written using both JUnit and TestNG. Although there are slight differences, both frameworks offer the most commonly-used functionalities, and the main difference is how tests are executed and organized.

Let's start with a question. What is a test? How can we define it?

Note

A test is a repeatable process or method that verifies the correct behavior of a tested target in a determined situation with a determined input expecting a predefined output or interactions.

In the programming approach, there are several types of tests depending on their scope: functional tests, acceptance tests, and unit tests. Further on, we will explore each of those types of tests in more detail.

Unit testing is about testing small pieces of code. Let's see how to test a single Java class. The class is quite...

Git


Git is the most popular revision control system. For that reason, all the code used in this book is stored in Bitbucket (https://bitbucket.org/). If you don't have it already, install Git. Distributions for all the popular operating systems can be found at http://git-scm.com.

Many graphical interfaces are available for Git; some of them being Tortoise (https://code.google.com/p/tortoisegit), Source Tree (https://www.sourcetreeapp.com), and Tower (http://www.git-tower.com/).

Virtual machines


Even though they are outside the topic of this book, virtual machines are a powerful tool and a first-class citizen in a good development environment. They provide dynamic and easy-to-use resources in isolated systems so they can be used and dropped at the time we need them. This helps developers to focus on their tasks instead of wasting their time creating or installing required services from scratch. This is the reason why virtual machines have found room in here. We want to take advantage of them to keep you focused on the code.

In order to have the same environment no matter the OS you're using, we'll be creating virtual machines with Vagrant and deploying required applications with Docker. We chose Ubuntu as a base operating system in our examples, just because it is a popular, commonly used Unix-like distribution. Most of these technologies are platform-independent, but occasionally you won't be able to follow the instructions found here because you might be using...

Build tools


With time, code tends to grow both in complexity and size. This occurs in the software industry by its nature. All products evolve constantly and new requirements are made and implemented across a product's life. Build tools offer a way to make managing project life cycle's as straightforward as possible, by following a few code conventions such as the organization of your code in a specific way and usage of naming a convention for your classes or a determined project structure formed by different folders and files.

Some of you might be familiar with Maven or Ant. They are a great couple of Swiss army knives for handling projects, but we are here to learn so we decided to use Gradle. Some of the advantages of Gradle are reduced boiler plate code, resulting in a much shortdr file and a more readable configuration file. Among others, Google uses it as its build tool. It is supported by IntelliJ IDEA and is quite easy to learn and work with. Most of the functionalities and tasks...

The integrated development environment


As many tools and technologies will be covered, we recommend using IntelliJ IDEA as the tool for code development. The main reason is that this IDE works without any tedious configuration. The Community Edition (IntelliJ IDEA CE) comes with a bunch of built-in features and plugins that make coding easy and efficient. It automatically recommends plugins that can be installed depending on the file extension. As IntelliJ IDEA is the choice we made for this book, you will find references and steps referring to its actions or menus. Readers should find a proper way to emulate those steps if they are using other IDEs. Refer to https://www.jetbrains.com/idea/ for instructions on how to download and install IntelliJ IDEA.

The IDEA demo project

Let's create the base layout of the demo project. This project will be throughout along this chapter to illustrate all the topics that are covered. Java will be the programming language and Gradle (http://gradle.org/) will...

Unit testing frameworks


In this section, two of the most used Java frameworks for unit testing are shown and briefly commented on. We will focus on their syntax and main features by comparing a test class written using both JUnit and TestNG. Although there are slight differences, both frameworks offer the most commonly-used functionalities, and the main difference is how tests are executed and organized.

Let's start with a question. What is a test? How can we define it?

Note

A test is a repeatable process or method that verifies the correct behavior of a tested target in a determined situation with a determined input expecting a predefined output or interactions.

In the programming approach, there are several types of tests depending on their scope: functional tests, acceptance tests, and unit tests. Further on, we will explore each of those types of tests in more detail.

Unit testing is about testing small pieces of code. Let's see how to test a single Java class. The class is quite simple,...

Hamcrest and AssertJ


In the previous section, we gave an overview of what a unit test is and how it can be written using two of the most commonly used Java frameworks. Since tests are an important part of our projects, why not improve the way we write them? Some cool projects emerged, aiming to empower the semantic of tests by changing the way assertions are made. As a result, tests are more concise and easier to understand.

Hamcrest

Hamcrest adds a lot of methods called matchers. Each matcher is designed to perform a comparison operation. It is extensible enough to support custom matchers created by yourself. Furthermore, JUnit supports Hamcrest natively since its core is included in the JUnit distribution. You can start using Hamcrest effortlessly. However, we want to use the full-featured project so we will add a test dependency to Gradle's file:

testCompile 'org.hamcrest:hamcrest-all:1.3'

Let us compare one assert from JUnit with the equivalent one from Hamcrest:

  • The JUnit assert:

         ...
Left arrow icon Right arrow icon

Description

Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to optimize it. The value of performing TDD with Java, one of the most established programming languages, is to improve the productivity of programmers, the maintainability and performance of code, and develop a deeper understanding of the language and how to employ it effectively. Starting with the basics of TDD and reasons why its adoption is beneficial, this book will take you from the first steps of TDD with Java until you are confident enough to embrace the practice in your day-to-day routine. You’ll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time. You’ll learn about the Red-Green-Refactor procedure, how to write unit tests, and how to use them as executable documentation. With this book you’ll also discover how to design simple and easily maintainable code, work with mocks, utilise behaviour-driven development, refactor old legacy code, and release a half-finished feature to production with feature toggles. You will finish this book with a deep understanding of the test-driven development methodology and the confidence to apply it to application programming with Java.

What you will learn

  • Explore the tools and frameworks required for effective TDD development
  • Perform the RedGreenRefactor process efficiently, the pillar around which all other TDD procedures are based
  • Master effective unit testing in isolation from the rest of your code
  • Design simple and easily maintainable codes by implementing different techniques
  • Use mocking frameworks and techniques to easily write and quickly execute tests
  • Develop an application to implement behaviourdriven development in conjunction with unit testing
  • Enable and disable features using Feature Toggles

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 27, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987436
Vendor :
Oracle
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 27, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987436
Vendor :
Oracle
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 164.97
Test-Driven Java Development
$54.99
Learning Java Functional Programming
$60.99
Learning Reactive Programming With Java 8
$48.99
Total $ 164.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Why Should I Care for Test-driven Development? Chevron down icon Chevron up icon
2. Tools, Frameworks, and Environments Chevron down icon Chevron up icon
3. Red-Green-Refactor – from Failure through Success until Perfection Chevron down icon Chevron up icon
4. Unit Testing – Focusing on What You Do and Not on What Has Been Done Chevron down icon Chevron up icon
5. Design – If It's Not Testable, It's Not Designed Well Chevron down icon Chevron up icon
6. Mocking – Removing External Dependencies Chevron down icon Chevron up icon
7. BDD – Working Together with the Whole Team Chevron down icon Chevron up icon
8. Refactoring Legacy Code – Making it Young Again Chevron down icon Chevron up icon
9. Feature Toggles – Deploying Partially Done Features to Production Chevron down icon Chevron up icon
10. Putting It All Together Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(13 Ratings)
5 star 46.2%
4 star 23.1%
3 star 7.7%
2 star 15.4%
1 star 7.7%
Filter icon Filter
Top Reviews

Filter reviews by




Pau Gonzalez Nov 05, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The best book on TDD. If after reading the book you still do not understand or do not want to apply TDD.., it's your fault.It contains very well explained code, talks about Unit tests, BDD, black box testing, whitebox ... some CI ...I highly recommend it.
Amazon Verified review Amazon
Amazon Customer Nov 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is an excellent book on TDD and Java. The book covers every aspect of TDD, and contains helpful information for both novices and experts.Although I don't use Java on a daily basis, the examples on the book are very easy to follow and understand. and the authors give very clear explanations that can be applied to any language.Very recommended for any software craftsman (or apprentice, as it is my case :) ).
Amazon Verified review Amazon
IGNASI MARIMON CLOS SUNYOL Oct 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've been developing in Java for the last 14 years and doing testing and then TDD for most of that time. Still, in this book I still learned some tricks and tools I'll add to my toolbox. I got the book to review the literature on the subject expecting just another intro to TDD. Instead, I've found that while introducing TDD, some more advanced aspects not usually mentioned are presented. For example, the importance of test method names.On each chapter, Viktor and Alex present just the minimum tools to achieve the goal they are focusing on and emphasize on the technique they want the reader to learn. This may seem irrelevant but I've found many people who struggle when they need to draw a line between integration and unit testing, or who fail to understand that when testing hurts it is a sign of a bad design. The book is rich on examples and code samples and as the authors put it: "It's not a book that one can read in bed. You'll have to get your hands dirty and code."Very recommendable to any self-respected java developer who wants to go a step forward.
Amazon Verified review Amazon
Cem Kaner Sep 11, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It is rare to see a good book on this topic written for inexperienced programmers. I can use it with first-year college students. You do need a basic knowledge of classes/objects to benefit from the book.
Amazon Verified review Amazon
Amazon Customer Apr 29, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
great book
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.