O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

TDD - Test Driven Development - Java JUnit FizzBuzz

1.756 visualizações

Publicada em

A short example Test Driven Development session where I code FizzBuzz.

FizzBuzz is often used as a programming interview question and as a Kata for practicing your coding.

The GitHub code repository with the Java code for this exercise is available at:


Read the blog post for the video:


Publicada em: Software
  • Seja o primeiro a comentar

TDD - Test Driven Development - Java JUnit FizzBuzz

  1. 1. 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
  2. 2. 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
  3. 3. 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
  4. 4. My First Test @Test public void fizzBuzzConvertorLeavesNormalNumbersAlone(){     FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();     Assert.assertEquals("1", fizzBuzz.convert(1)); }
  5. 5. 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));
  6. 6. This forced me to actually implement the default code in convert: return String.valueOf(toConvertToFizzBuzz);
  7. 7. My Second Test The second test was: @Test public void fizzBuzzConvertorMultiplesOfThree(){     FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();     Assert.assertEquals("Fizz", fizzBuzz.convert(3)); }
  8. 8. This forced me to implement the '3' division rule: if(toConvertToFizzBuzz%3==0){    return "Fizz"; }
  9. 9. My Third Test @Test public void fizzBuzzConvertorMultiplesOfFive(){     FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();     Assert.assertEquals("Buzz", fizzBuzz.convert(5)); }
  10. 10. Much the same as the condition for number 3: if(toConvertToFizzBuzz%5==0){    return "Buzz"; }
  11. 11. 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); }
  12. 12. My Fourth Test @Test public void multiplesOfBothThreeAndFive(){    FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();    Assert.assertEquals("FizzBuzz", fizzBuzz.convert(15)); }
  13. 13. 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"; }
  14. 14. 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); }
  15. 15. @Test public void outputTheHundredFizzBuzzes(){    FizzBuzzConverter fizzBuzz = new FizzBuzzConverter();     for(int i=1; i<=100; i++){        System.out.println(fizzBuzz.convert(i));     } }
  16. 16. 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
  17. 17. End Alan Richardson www.compendiumdev.co.uk Linkedin ‐ @eviltester Twitter ‐ @eviltester Instagram ‐ @eviltester Facebook ‐ @eviltester Youtube ‐ EvilTesterVideos Pinterest ‐ @eviltester Github ‐ @eviltester Slideshare ‐ @eviltester
  18. 18. 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