Test List (Requirements)

  • When a number is divisible by 3 return Fizz
  • When a number is divisible by 5 return Buzz
  • When a number is divisible by 3 & 5 return FizzBuzz
  • All other numbers return that number
  • Only accept whole integers in the range 0 to Integer.MAX
TDD Cycle

Step 3 - Write enough code to pass the test

  • Write only enough Production code to pass the test and no more. (This can be hard to do, but resist for now)
  • Run the test - It should pass (Instance feedback & a sense of progress)

The least amount of code that can be written to pass the test is to simply 'return "FIZZ";'
This is how TDD helps reduce complexity & produce a well tested system, we only write what we need for the test to pass.

public class FizzBuzzProcessorTest {
    @Test
    public void shouldReturnFizzWhenNumberDivisibleBy3() {
        // Multiple asserts, but all with a single concept
        assertEquals( "Fizz", FizzBuzzProcessor.processNumber( 3 ) );
        assertEquals( "Fizz", FizzBuzzProcessor.processNumber( 6 ) );
        assertEquals( "Fizz", FizzBuzzProcessor.processNumber( 9 ) );
    }
}
                                
public class FizzBuzzProcessor {
    public static String processNumber( int number ) {
        return "Fizz";
    }
}
                                

Test Results: PASS (1 pass, 0 fail)

Comments

It seems wrong to write code we know we are going to replace and in reality we may not go quite to this extreme, but the point is valid.
  • No unnecessary code (or untested code)
  • Keep the solution simple as possible
  • Refrain from running away with coding more than is required (at this point in time)

Refrain from trying to code it all in one go, we are aiming for each use case to be covered by a test and to build up the solution gradually