Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get Started With Test Driven Development (A Beginner's Guide)

DZone's Guide to

Get Started With Test Driven Development (A Beginner's Guide)

Learn what Test Driven Development is about, how good unit testing fits in, and why it can get you a shorter development cycle and better results.

· DevOps Zone ·
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

Wondering what Test Driven Development looks like? Then this is for you. Why not give TDD a try and see how you like it?

Test Driven Development illustrationSo What Is TDD?

Can you actually write a test before writing a single line of implementation logic? How do you know what method, class, or interface will contain the method under test? Sounds a bit odd… That’s exactly how I felt when I first read about TDD.

According to Wikipedia: "Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only."

But once I got the hang of it I realized, that’s the whole point! It guides you through the design.

The TDD concept was introduced by Kent Beck, and in his book "Test Driven Development: By Example," he explains the pains of the traditional development process and how adopting test driven development helps. Also, the foundation and key concepts are explained with lots of code samples.

Let's Test the Waters with a Hello World in TDD

All that is great! But how do you practice TDD in Java development? Well, I started by applying it to a "Hello World." Too trivial, I know, but I like it to be as simple as possible.

Requirement: I want to print "hello world."

NOTE: Please head over to https://javacodehouse.com/tdd-tutorial to see this in action with full animated GIFs.

1 | Writing the Test

We think of the outcome first, and write an assert to test if we get what we want. In this case, we want the string "hello world."

Image title

2 | Writing the Minimum Code in Order to Pass the Test

Image title

3 | Refactor Both the Test and the Implementation

Image title

It’s that simple. So what’s keeping you from practicing TDD? Let’s get started.

For the majority of us who are so used to writing the method or function first and writing the unit tests later, or may be skipping it altogether, it may feel like swimming upstream, but like anything, a bit of practice will make it a lot easier and more natural.

Following are a few things that motivated me and planted the foundation of TDD in myself:

  • "Test Driven Development: By Example" by Kent Beck- Reading this book set my mind up for it and it really extracts the essence of test driven development.
  • Writing great unit tests, i.e. simple, understandable, and maintainable unit tests.
  • TDD Kata- Small practice exercises that help you master it.

Quality Matters

Yes, the quality of unit tests does matter as much as the quality of production code.

Why Is It So Important to Write Good Unit Tests?

If unit tests lack quality (i.e. if the tests are not easily understandable by a person other than the one who wrote it, or if they inter depend on other tests causing multiple tests to fail if one test is broken, or if unit test suit take ages to run, etc.) maintaining those unit tests would become a nightmare in the long run and would ultimately lead to ignoring all the tests one by one as they fail. Don’t believe me? Then you may want to read this open letter from an ignored test.

Some qualities of a great unit test:

  • Each unit test should be able to run independently of other tests.
  • Each test should test just one thing (single behavior, method, or function).
  • All external dependencies should be mocked.
  • The assumptions for each test should be clear and defined within the test method.
  • The name of the test method should be meaningful.
  • Unit tests should be fast, so that the can be run as often as required.

TDD Kata - Practice Makes Perfect

Like anything, the key to TDD is practice. But how do you practice? TDD Katas to the rescue.

KATA has its roots in Japan, and it means a detailed or defined set of movements to be practiced over and over again.

TDD Kata is a tiny bit of coding that you could repeat over and over again, not a new one every day but the same coding exercise until you get sick of it. So, head over to this repository where you can find a list of Katas to practice and start now.

Please leave a comment and let me know if you liked it!

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
tdd ,unit testing ,junit ,devops ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}