Test-Driven Java Development, Second Edition
eBook - ePub

Test-Driven Java Development, Second Edition

Viktor Farcic, Alex Garcia

Share book
  1. 324 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Test-Driven Java Development, Second Edition

Viktor Farcic, Alex Garcia

Book details
Book preview
Table of contents
Citations

About This Book

This book will teach the concepts of test driven development in Java so you can build clean, maintainable and robust code

Key Features

  • Explore the most popular TDD tools and frameworks and become more proficient in building applications
  • Create applications with better code design, fewer bugs, and higher test coverage, enabling you to get them to market quickly
  • Implement test-driven programming methods into your development workflows

Book Description

Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasizes 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 longest established programming languages, is to improve the productivity of programmers and 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 understanding 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 we will dive right into 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, utilize behavior-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 Red-Green-Refactor 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 code by implementing different techniques
  • Use mocking frameworks and techniques to easily write and quickly execute tests
  • Develop an application to implement behavior-driven development in conjunction with unit testing
  • Enable and disable features using feature toggles

Who this book is for

If you're an experienced Java developer and want to implement more effective methods of programming systems and applications, then this book is for you.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Test-Driven Java Development, Second Edition an online PDF/ePUB?
Yes, you can access Test-Driven Java Development, Second Edition by Viktor Farcic, Alex Garcia in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation Web. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788832120
Edition
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 their features, advantages, and disadvantages. Let's get 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
  • Hamcrest and AssertJ
  • Code coverage tools
  • Mocking frameworks
  • User interface testing
  • Behavior-driven development

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 some other operating system. In that case, your task is to find what the differences are between Ubuntu and your operating system and act accordingly.

Vagrant

Vagrant is the tool we are going to use for creating the development environment stack. It is an easy way to initialize ready-to-go virtual machines with minimum effort using preconfigured boxes. All boxes and configurations are placed in one file, called the Vagrant file.
Here is an example of creating a simple Ubuntu box. We made an extra configuration for installing MongoDB using Docker (the usage of Docker will be explained shortly). We assume that you have VirtualBox (https://www.virtualbox.org) and Vagrant (https://www.vagrantup.com) installed on your computer and that you have internet access.
In this particular case, we are creating an instance of Ubuntu 64-bits using the Ubuntu box (ubuntu/trusty64) and specifying that the VM should have 1 GB of RAM:
 config.vm.box = "ubuntu/trusty64" config.vm.provider "virtualbox" do |vb| vb.memory = "1024" end 
Further on, we're exposing MongoDB's default port in the Vagrant machine and running it using Docker:
 config.vm.network "forwarded_port", guest: 27017, host: 27017 config.vm.provision "docker" do |d| d.run "mongoDB", image: "mongo:2", args: "-p 27017:27017" end 
Finally, in order to speed up the Vagrant setup, we're caching some resources. You should install the plugin called cachier. For further information, visit: https://github.com/fgrehm/vagrant-cachier.
 if Vagrant.has_plugin?("vagrant-cachier") config.cache.scope = :box end 
Now it's time to see it working. It usually takes a few minutes to run it for the first time because the base box and all the dependencies need to be downloaded and installed:
$> vagrant plugin install vagrant-cachier $> git clone https://bitbucket.org/vfarcic/tdd-java-ch02-example-vagrant.git $> cd tdd-java-ch02-example-vagrant
$> vagrant up
When this command is run, you should see the following output:
Be patient until the execution is finished. Once done, you'll have a new virtual machine with Ubuntu, Docker, and one MongoDB instance up and running. The best part is that all this was accomplished with a single command.
To see the status of the currently running VM, we can use the status argument:
$> vagrant status Current machine states: default running (virtualbox) 
The virtual machine can be accessed either through ssh or by using Vagrant commands, as in the following example:
$> vagrant ssh Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-46-generic x86_64)  * Documentation: https://help.ubuntu.com/  System information disabled due to load higher than 1.0  Get cloud support with Ubuntu Advantage Cloud Guest:  http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. 
vagrant@vagrant-ubuntu-trusty-64:~$
Finally, to stop the virtual machine, exit from it and run the vagrant halt command:
$> exit $> vagrant halt  ==> default: Attempting graceful shutdown of VM... $> 
For the list of Vagrant boxes or further details about configuring Vagrant, visit: https://www.vagrantup.com.

Docker

Once the environment is set, it is time to install the services and the software that we need. This can be done using Docker, a simple and portable way to ship and run many applications and services in isolated containers. We will use it to install the required databases, web servers, and all the other applications required throughout this book, in a virtual machine created using Vagrant. In fact, the Vagrant VM that was previously created already has an example of getting an instance of MongoDB up and running using Docker.
Let's bring up the VM again (we stopped it previously with the vagrant halt command) and also MongoDB:
$> vagrant up $> vagrant ssh vagrant@vagrant-ubuntu-trusty-64:~$ docker start mongoDB
mongoDB
vagrant@vagrant-ubuntu-trusty-64:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED 360f5340d5fc mongo:2 "/entrypoint.sh mong..." 4 minutes ago STATUS PORTS NAMES Up 4 minutes 0.0.0.0:27017->27017/tcp mongoDB vagrant@vagrant-ubuntu-trusty-64:~$ exit
With docker start, we started the container; with docker ps, we listed all the running processes.
By using this kind of procedure, we are able to reproduce a full-stack environment in the blink of an eye. You may be wondering if this is as awesome as it sounds. The answer is yes, it is. Vagrant and Docker allow developers to focus on what they are supposed to do and forget about complex installations and tricky configurations. Furthermore, we made an extra effort to provide you with all the necessary steps and resources to reproduce and test all the code examples and demonstrations in this book.

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 the project life cycle as straightforward as possible, by following a few code conventions, such as the organization of your code, in a specific way, and by the 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 its reduced boilerplate code, resulting in a much shorter file and a more ...

Table of contents