Fizz Buzz Test Driven Development Task:
Create the Fizz Buzz game using Test Driven Developoment. Print the correct word for range 1-100.

The implementation:

public class Main {

    static final String FIZZ = "Fizz";
    static final String BUZZ = "Buzz";
    static final String FIZZ_BUZZ = "Fizz Buzz";

    public static void main(String[] args) {
        IntStream.range(1, 101).forEach((x) -&gt; System.out.println(getWordForNumber(x)));
    }

    public static String getWordForNumber(int x) {
        if (isDivisibleWithoutRemainder(x, 15)) {
            return FIZZ_BUZZ;
        } else if(isDivisibleWithoutRemainder(x, 3)) {
            return FIZZ;
        } else if(isDivisibleWithoutRemainder(x, 5)) {
            return BUZZ;
        }
        return Integer.toString(x);
    }

    private static  boolean isDivisibleWithoutRemainder(int dividend, int divisor) {
        return (dividend%divisor == 0);
    }

}


The JUnit Test:

final class Number {
    private final int n;

    Number (int number) {
      n = number;
    }

    final boolean isMatchedToWord(final String word) {
      return Main.getWordForNumber(n).equals(word);
    }
}

class MainTest {
    @Test
    public void numbersAreMatchedToWords_Works() {
      assertTrue(new Number(1).isMatchedToWord("1"));
      assertTrue(new Number(2).isMatchedToWord("2"));
      assertTrue(new Number(3).isMatchedToWord("Fizz"));
      assertTrue(new Number(4).isMatchedToWord("4"));
      assertTrue(new Number(5).isMatchedToWord("Buzz"));
      assertTrue(new Number(6).isMatchedToWord("Fizz"));
      assertTrue(new Number(10).isMatchedToWord("Buzz"));
      assertTrue(new Number(15).isMatchedToWord("Fizz Buzz"));
      assertTrue(new Number(30).isMatchedToWord("Fizz Buzz"));
    }
}
Test driven development

In test driven development you let the code and the tests evolve by repeating the following steps:


Add a new test case that fails, the simplest possible
Fix the implementation the simplest possible way so that all test cases pass
Optionally refactor the existing code, while keeping all tests passing
Repeat from step 1


There are variations to these steps, but one thing is certain: in the end you will have many test cases. The posted code has one, which means it wasn't really developed using TDD.

Keep it simple

It's good to keep everything as simple as possible. In the test class, do you really need a Number class to wrap an number, so that you create an instance and then call a method on that instance? A simple method would have been enough, that takes an integer as parameter and returns a string.