- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Test-driven Development While Building a Set Class
In this Code Lab, you will learn the fundamentals of Test-driven Development (TDD) by building a custom set implementation in Java. You will follow the Red-Green-Refactor cycle, using a pre-written test suite to guide your development process incrementally.
Lab Info
Table of Contents
-
Challenge
Introduction
Welcome to this Guided Code Lab on Test-driven Development (TDD)! The core idea of TDD is to let tests guide your implementation. This is done through a short, repetitive cycle:
- Red: Write a test that defines a small piece of desired functionality. Run it and watch it fail. This is expected, as you haven't written the implementation code yet.
- Green: Write the simplest, most minimal code possible to make the failing test pass. Run the test again and see it turn green.
- Refactor: With the safety of a passing test, you can now clean up your implementation code, improve its structure, or remove duplication without changing its external behavior.
In this lab, the tests are already written for you. Your job is to follow the cycle by implementing the code to make the tests pass, one by one. You can run the tests at any time by executing
mvn testin the Terminal. You will see many tests fail initially—this is the Red phase!It's time to get started. info> This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Getting to Green with `isEmpty` and `size`
You will get started by implementing the most basic functionality: checking if the set is empty and reporting its size. These methods form the foundation of your
CustomSetclass.You'll work through the TDD cycle: watch a test fail (Red), implement the minimal code to make it pass (Green), and then move on to the next test.
-
Challenge
Adding Elements and Handling Duplicates
Your set can now report its size, but it's not very useful without the ability to add elements. In this step, you'll implement the
add()method. You will do this in two stages, perfectly demonstrating the TDD cycle.First, you will write the simplest code to add an element. This will satisfy the first
addtest. Then, you will see a second test that fails because it tries to add a duplicate element. This will force you to go back and refactor youraddmethod to be smarter, completing the Red-Green-Refactor loop. A key characteristic of a Set is that it does not allow duplicate elements, but your current implementation doesn't enforce this rule, so the duplicate-element test is failing. -
Challenge
Querying the Set with `contains()`
Adding elements is great, but a key function of a set is to be able to quickly check if it contains a specific item. You will now implement the
contains()method. The test for this is straightforward: after adding an element,contains()should returntruefor that element andfalsefor an element that has not been added. Your implementation should satisfy both of these conditions. -
Challenge
Removing Elements
You've reached the final piece of core functionality: removing elements from the set. Following the TDD pattern, you'll find a test waiting for you that adds an element, removes it, and then asserts that the set is empty and no longer contains the element. Your goal is to write the implementation for the
remove()method to make this final test pass. -
Challenge
Conclusion
Congratulations! You have successfully built a
CustomSetclass from scratch using the Test-driven Development methodology.Throughout this lab, you've practiced the Red-Green-Refactor cycle by:
- Starting with failing tests (Red phase)
- Writing minimal code to make tests pass (Green phase)
- Refactoring to improve code quality while maintaining passing tests
You've learned how tests can guide your implementation, help you add features incrementally, and give you confidence when refactoring. This test-first approach helps create code that is not only correct by definition, but also more robust and easier to maintain.
By mastering TDD, you've gained a powerful tool for building reliable, well-structured software.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.