Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a quick Fizz Buzz exercise I did to teach a high school student the concepts behind TDD. Method naming and code structure could be better, but I just needed to illustrate a point. I felt the exercise went well and I thought it might be worth while to keep the code.
package com.wanooknox.fizzbuzz;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FizzBuzzTest {
private FizzBuzzEngine fizzBuzzEngine;
@Before
public void setup() {
fizzBuzzEngine = new FizzBuzzEngine();
}
@Test
public void isMultipleOf3_shouldReturnTrue_whenGivenExactly3() {
// Setup
boolean expected = true;
int input = 3;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf3(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf3_shouldReturnTrue_whenGiven6() {
// Setup
boolean expected = true;
int input = 6;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf3(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf3_shouldReturnFalse_whenGiven5() {
// Setup
boolean expected = false;
int input = 5;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf3(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf5_shouldReturnTrue_whenGivenExactly5() {
// Setup
boolean expected = true;
int input = 5;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf5(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf5_shouldReturnTrue_whenGiven10() {
// Setup
boolean expected = true;
int input = 10;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf5(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf5_shouldReturnFalse_whenGiven3() {
// Setup
boolean expected = false;
int input = 3;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf5(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void isMultipleOf5_shouldReturnFalse_whenGiven9() {
// Setup
boolean expected = false;
int input = 9;
// Execute
boolean actual = fizzBuzzEngine.isMultipleOf5(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void fizzOrBuzzOrNumber_shouldReturnFizz_whenMultipleOf3() {
// Setup
String expected = "Fizz";
int input = 6;
// Execute
String actual = fizzBuzzEngine.fizzOrBuzzOrNumber(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void fizzOrBuzzOrNumber_shouldReturnBuzz_whenMultipleOf5() {
// Setup
String expected = "Buzz";
int input = 10;
// Execute
String actual = fizzBuzzEngine.fizzOrBuzzOrNumber(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void fizzOrBuzzOrNumber_shouldReturnFizzBuzz_whenMultipleOf3And5() {
// Setup
String expected = "FizzBuzz";
int input = 15;
// Execute
String actual = fizzBuzzEngine.fizzOrBuzzOrNumber(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void fizzOrBuzzOrNumber_shouldReturnTheNumber_whenNotMultipleOf3Or5() {
// Setup
String expected = "14";
int input = 14;
// Execute
String actual = fizzBuzzEngine.fizzOrBuzzOrNumber(input);
// Assert
assertEquals(expected, actual);
}
@Test
public void dummy() {
fizzBuzzEngine.doFizzBuzz();
}
private class FizzBuzzEngine {
public void doFizzBuzz() {
for (int i = 0; i < 100; i++) {
System.out.println(fizzOrBuzzOrNumber(i));
}
}
public String fizzOrBuzzOrNumber(int input) {
String result = "";
if (isMultipleOf3(input)) {
result += "Fizz";
}
if (isMultipleOf5(input)) {
result += "Buzz";
}
if (result == "") {
return String.valueOf(input);
} else {
return result;
}
}
public boolean isMultipleOf3(int value) {
int divisor = 3;
return ((value % divisor) == 0);
}
public boolean isMultipleOf5(int value) {
return ((value % 5) == 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.