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
zł59.99 zł177.99
Paperback
zł221.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781783987429
Vendor :
Oracle
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 27, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987429
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 zł20 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 zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 666.97
Test-Driven Java Development
zł221.99
Learning Java Functional Programming
zł246.99
Learning Reactive Programming With Java 8
zł197.99
Total 666.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.