TDD — Test-driven FizzBuzz

Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
}
Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
}

private String fizzBuzz(int input) {
return "1";
}
Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
assertEquals("2", fizzBuzz(2));
}
Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
assertEquals("2", fizzBuzz(2));
}

private String fizzBuzz(int input) {
return input == 2 ? "2" : "1";
}
Image for post
Image for post
private String fizzBuzz(int input) {
return String.valueOf(input);
}
Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
assertEquals("2", fizzBuzz(2));
assertEquals("Fizz", fizzBuzz(3));
assertEquals("Fizz", fizzBuzz(6));
assertEquals("Fizz", fizzBuzz(9));
}
Image for post
Image for post
private String fizzBuzz(int input) {
if (input % 3 == 0) {
return "Fizz";
}
return String.valueOf(input);
}
Image for post
Image for post
@Test
public void testFizzBuzz() {
assertEquals("1", fizzBuzz(1));
assertEquals("2", fizzBuzz(2));
assertEquals("Fizz", fizzBuzz(3));
assertEquals("Fizz", fizzBuzz(6));
assertEquals("Fizz", fizzBuzz(9));
assertEquals("Buzz", fizzBuzz(5));
assertEquals("Buzz", fizzBuzz(10));
}
Image for post
Image for post
private String fizzBuzz(int input) {
if (input % 3 == 0) {
return "Fizz";
}
if (input % 5 == 0) {
return "Buzz";
}
return String.valueOf(input);
}
Image for post
Image for post
private String fizzBuzz(int input) {
if (isDivisibleBy(input, 3)) {
return "Fizz";
}
if (isDivisibleBy(input, 5)) {
return "Buzz";
}
return String.valueOf(input);
}

private boolean isDivisibleBy(int dividend, int divisor) {
return dividend % divisor == 0;
}
Image for post
Image for post
private String fizzBuzz(int input) {
if (input % 3 == 0) {
return "Fizz";
}
if (input % 5 == 0) {
return "Buzz";
}
return String.valueOf(input);
}
assertEquals("FizzBuzz", fizzBuzz(15));
Image for post
Image for post
private String fizzBuzz(int input) {
if (input % 3 == 0 && input % 5 == 0) {
return "FizzBuzz";
}
if (input % 3 == 0) {
return "Fizz";
}
if (input % 5 == 0) {
return "Buzz";
}
return String.valueOf(input);
}
Image for post
Image for post
private String fizzBuzz(int input) {
String output = "";
if (input % 3 == 0) {
output = output.concat("Fizz");
}
if (input % 5 == 0) {
output = output.concat("Buzz");
}
if (!output.equals("")) {
return output;
}
return String.valueOf(input);
}
Image for post
Image for post
private String fizzBuzz(int input) {
String output = "";
if (input % 3 == 0) {
output += "Fizz";
}
if (input % 5 == 0) {
output += "Buzz";
}
if (input % 3 != 0 && input % 5 != 0) {
output = String.valueOf(input);
}
return output;
}
Image for post
Image for post

Conclusion and Remarks!

assertEquals("1, 2, Fizz, 4, Buzz, Fizz", fizzBuzz(6));
Image for post
Image for post
Image for post
Image for post

Software Engineer. For updates follow https://twitter.com/ronnieschaniel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store