DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Best Practices for Writing Unit Tests: A Comprehensive Guide
  • Exploring Unit Testing in Golang
  • To Test or Not to Test: Finding the Right Balance in Unity Game Development

Trending

  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • Why Good Models Fail After Deployment
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Get Started With Test Driven Development (A Beginner's Guide)

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.

By 
Dilini Rajapaksha user avatar
Dilini Rajapaksha
·
Sep. 06, 17 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free
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!

unit test Test-driven development

Published at DZone with permission of Dilini Rajapaksha. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Best Practices for Writing Unit Tests: A Comprehensive Guide
  • Exploring Unit Testing in Golang
  • To Test or Not to Test: Finding the Right Balance in Unity Game Development

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook