0:0033:23

Test Driven Development (TDD) in practice

Sumana Saha 33:23 2021

Write the test before the code, and let the failure tell you what to build

A blank JUnit test passes, and so does a validator hard-coded to return true, so the real work starts when a phone number without a country code slips through as valid. Software engineer Sumana Saha of Ratepay GmbH builds a Spring Boot validator one failing test at a time. What remains is where that red-green-refactor rhythm helps a team and where it starts to hurt.

Learn the red-green-refactor rules, the given-when-then test structure and where TDD falls short

Watch on YouTube

In short

Sumana Saha, a Software Engineer at Ratepay GmbH, explains test-driven development and its red-green-refactor cycle, then demonstrates it by building a phone number validator test-first in a Java Spring Boot project with JUnit 5 and IntelliJ IDEA. She links TDD to agile incremental delivery, weighs its benefits against costs such as tests tightly coupled to implementation, and recommends resources for getting started.

“So we are always here not to write garbage code. We should follow the clean code principle that how much is needed.”
“This is the fail fast approach. That means we know the test is failing.”

What you will learn

  • Apply the red-green-refactor cycle: write a failing unit test, write just enough code to pass, then refactor and repeat.
  • Structure unit tests with given-when-then and a setup method annotated with @BeforeEach in JUnit 5.
  • Separate production code in the main package from test code in the test package of a Maven Spring Boot project.
  • Use invalid and edge-case test data to expose bugs that a happy-path test misses.
  • Weigh TDD's costs, such as tests tightly coupled to implementation and a steep start for newcomers, against its fail-fast benefits.

Key takeaways from the speaker

  • Software testing automation
  • Microservices testing methodologies
  • Mockito framework

Details

Level
Intermediate
Format
Tech talk
Technical depth
Practitioner
For
Early career, Mid-career
Length
33:23
Published
June 2021

Questions this talk answers

What is test-driven development?

It is a practice where you write a test before any production code, watch it fail, write the simplest code to make it pass, then refactor. This back-and-forth loop is known as red-green-refactor.

Where did TDD come from?

It began as test-first development, and as the speaker cited, Kent Beck coined the term test-driven development in 2003.

Why are TDD and agile so often paired?

Agile assumes requirements change constantly and delivers incrementally, and TDD supplies a quality technique for that incremental development.

How should a unit test be structured?

Follow given-when-then: provide test data, call the production method, then assert on a variable named actual. Use a setup method annotated with @BeforeEach to create a fresh instance for each test.

What are the downsides of TDD?

Tests can be tightly coupled to implementation details and fail unexpectedly when code is refactored, and newcomers need deep knowledge of the internal architecture. Unspecified features can also be missed, so good documentation of use cases helps.

About this talk

TDD is a design technique that guides the development of software through testing. With TDD, test cases are written before the code which they test, in a technique called test-first programming, which forces the developer to think about what could possibly go wrong even before the implementation itself begins. Experiments and observations in the industry have shown that TDD increases code quality and reduces defect density. In this session, we will see how exactly we can achieve this design technique for a working software / API. The idea behind Red-Green-Refactor will be discussed and shown with examples. Prerequisite : Basic programming knowledge in Java, IntelliJ IDEA, Spring Boot

Topics

Related talks

Browse the library