SlideShare a Scribd company logo
TDD - Test Driven Development - Java JUnit FizzBuzz
TDD ‐ Test Driven Development ‐ Java
JUnit FizzBuzz
Alan Richardson
www.eviltester.com/agile
www.compendiumdev.co.uk
@eviltester
Source Code:
https://github.com/eviltester/fizzbuzz
BlogPost:
http://blog.eviltester.com/2018/03/tdd‐test‐driven‐development‐
java‐junit.html
What is FizzBuzz
// Write a program that prints the numbers from 1 to 100.
// for multiples of three print “Fizz” instead of the number
// and for the multiples of five print “Buzz”.
// For numbers multiples of both 3 and 5 print “FizzBuzz”
http://wiki.c2.com/?FizzBuzzTest
First I:
created a test class
copied in the rules as a comment
formatted the rules to make it easy to understand
added some examples so that I could understand
My First Test
@Test
public void fizzBuzzConvertorLeavesNormalNumbersAlone(){
    FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();
    Assert.assertEquals("1", fizzBuzz.convert(1));
}
This forced me to create the  FizzBuzzConverter class and  convert 
method.
I added a second assertion to this test:
Assert.assertEquals("2", fizzBuzz.convert(2));
This forced me to actually implement the default code in convert:
return String.valueOf(toConvertToFizzBuzz);
My Second Test
The second test was:
@Test
public void fizzBuzzConvertorMultiplesOfThree(){
    FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();
    Assert.assertEquals("Fizz", fizzBuzz.convert(3));
}
This forced me to implement the '3' division rule:
if(toConvertToFizzBuzz%3==0){
   return "Fizz";
}
My Third Test
@Test
public void fizzBuzzConvertorMultiplesOfFive(){
    FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();
    Assert.assertEquals("Buzz", fizzBuzz.convert(5));
}
Much the same as the condition for number 3:
if(toConvertToFizzBuzz%5==0){
   return "Buzz";
}
At this point my  convert method looks
as follows:
public String convert(int toConvertToFizzBuzz) {
    if(toConvertToFizzBuzz%5==0){
       return "Buzz";
    }
    
    if(toConvertToFizzBuzz%3==0){
       return "Fizz";
    }
    
    return String.valueOf(toConvertToFizzBuzz);
}
My Fourth Test
@Test
public void multiplesOfBothThreeAndFive(){
   FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();
   Assert.assertEquals("FizzBuzz", fizzBuzz.convert(15));
}
Avoid Temptation to Over‐Complicate
should I add a flag to check for fizz and buzz?
should I have a set of nested ifs?
perhaps I can use a tertiary operator for some 'magic'
keep it simple:
if(toConvertToFizzBuzz%15==0){
   return "FizzBuzz";
}
public String convert(int toConvertToFizzBuzz) {
    if(toConvertToFizzBuzz%15==0){
       return "FizzBuzz";
    }
    
    if(toConvertToFizzBuzz%5==0){
        return "Buzz";
    }
    
    if(toConvertToFizzBuzz%3==0){
        return "Fizz";
    }
    
    return String.valueOf(toConvertToFizzBuzz);
}
@Test
public void outputTheHundredFizzBuzzes(){
   FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();
    for(int i=1; i<=100; i++){
       System.out.println(fizzBuzz.convert(i));
    }
}
Source Code:
https://github.com/eviltester/fizzbuzz
BlogPost with more explanation:
http://blog.eviltester.com/2018/03/tdd‐test‐driven‐development‐
java‐junit.html
YouTube Video:
https://youtu.be/DWQgJqzkhvk
End
Alan Richardson www.compendiumdev.co.uk
Linkedin ‐ @eviltester
Twitter ‐ @eviltester
Instagram ‐ @eviltester
Facebook ‐ @eviltester
Youtube ‐ EvilTesterVideos
Pinterest ‐ @eviltester
Github ‐ @eviltester
Slideshare ‐ @eviltester
BIO
Alan is a test consultant who works at a technical level using
techniques from psychotherapy and computer science. In his spare
time Alan is currently programming a multi‐user text adventure
game, a Twitter Client and some buggy JavaScript games in the
style of the Cascade Cassette 50. Alan is the author of the books
"Dear Evil Tester", "Java For Testers" and "Automating and Testing a
REST API". Alan's main website is compendiumdev.co.uk and he
mostly blogs at blog.eviltester.com

More Related Content

Similar to TDD - Test Driven Development - Java JUnit FizzBuzz (11)

PDF
Kata de TDD by Jordi Anguela
Codium
 
PDF
Exploring type-directed, test-driven development: a case study using FizzBuzz
Franklin Chen
 
PDF
Test Driven Development with Fizz Buzz by Brian Bayer
QA or the Highway
 
PDF
Basic TDD moves
Fernando Cuenca
 
PDF
"How keep normal blood pressure using TDD" By Roman Loparev
Ciklum Ukraine
 
PPTX
The TDD game that you never played
Lior Israel
 
PDF
Java Beginners Meetup February 2017: Testing and TDD
Patrick Kostjens
 
PDF
TDD Introduction with Kata FizzBuzz
Rick Liu
 
PDF
Introducción práctica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
 
PDF
Introducción práctica a TDD
rubocoptero
 
PDF
Mobile Development - Unit and Automation Testing
Manuel Vicente Vivo
 
Kata de TDD by Jordi Anguela
Codium
 
Exploring type-directed, test-driven development: a case study using FizzBuzz
Franklin Chen
 
Test Driven Development with Fizz Buzz by Brian Bayer
QA or the Highway
 
Basic TDD moves
Fernando Cuenca
 
"How keep normal blood pressure using TDD" By Roman Loparev
Ciklum Ukraine
 
The TDD game that you never played
Lior Israel
 
Java Beginners Meetup February 2017: Testing and TDD
Patrick Kostjens
 
TDD Introduction with Kata FizzBuzz
Rick Liu
 
Introducción práctica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
 
Introducción práctica a TDD
rubocoptero
 
Mobile Development - Unit and Automation Testing
Manuel Vicente Vivo
 

More from Alan Richardson (20)

PDF
Add More Security To Your Testing and Automating - Saucecon 2021
Alan Richardson
 
PDF
Automating to Augment Testing
Alan Richardson
 
PDF
Open source tools - Test Management Summit - 2009
Alan Richardson
 
PDF
Automating Tactically vs Strategically SauceCon 2020
Alan Richardson
 
PDF
The Future of Testing Webinar
Alan Richardson
 
PDF
Devfest 2019-slides
Alan Richardson
 
PDF
Secrets and Mysteries of Automated Execution Keynote slides
Alan Richardson
 
PDF
Automating Pragmatically - Testival 20190604
Alan Richardson
 
PDF
Joy of Coding Conference 2019 slides - Alan Richardson
Alan Richardson
 
PDF
Programming katas for Software Testers - CounterStrings
Alan Richardson
 
PDF
Technology Based Testing
Alan Richardson
 
PDF
About Consultant Alan Richardson Compendium Developments Evil Tester
Alan Richardson
 
PDF
Shift left-testing
Alan Richardson
 
PDF
Automating and Testing a REST API
Alan Richardson
 
PDF
Technical and Testing Challenges: Using the "Protect The Square" Game
Alan Richardson
 
PDF
If you want to automate, you learn to code
Alan Richardson
 
PDF
How To Test With Agility
Alan Richardson
 
PDF
Your Automated Execution Does Not Have to be Flaky
Alan Richardson
 
PDF
What is Testability vs Automatability? How to improve your Software Testing.
Alan Richardson
 
PDF
What is Agile Testing? A MindMap
Alan Richardson
 
Add More Security To Your Testing and Automating - Saucecon 2021
Alan Richardson
 
Automating to Augment Testing
Alan Richardson
 
Open source tools - Test Management Summit - 2009
Alan Richardson
 
Automating Tactically vs Strategically SauceCon 2020
Alan Richardson
 
The Future of Testing Webinar
Alan Richardson
 
Devfest 2019-slides
Alan Richardson
 
Secrets and Mysteries of Automated Execution Keynote slides
Alan Richardson
 
Automating Pragmatically - Testival 20190604
Alan Richardson
 
Joy of Coding Conference 2019 slides - Alan Richardson
Alan Richardson
 
Programming katas for Software Testers - CounterStrings
Alan Richardson
 
Technology Based Testing
Alan Richardson
 
About Consultant Alan Richardson Compendium Developments Evil Tester
Alan Richardson
 
Shift left-testing
Alan Richardson
 
Automating and Testing a REST API
Alan Richardson
 
Technical and Testing Challenges: Using the "Protect The Square" Game
Alan Richardson
 
If you want to automate, you learn to code
Alan Richardson
 
How To Test With Agility
Alan Richardson
 
Your Automated Execution Does Not Have to be Flaky
Alan Richardson
 
What is Testability vs Automatability? How to improve your Software Testing.
Alan Richardson
 
What is Agile Testing? A MindMap
Alan Richardson
 
Ad

Recently uploaded (20)

PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Ad

TDD - Test Driven Development - Java JUnit FizzBuzz