Back to Repositories

Testing FizzBuzz Algorithm Implementation in FizzBuzzEnterpriseEdition

This comprehensive test suite validates the FizzBuzz implementation using JUnit and Spring framework integration. The tests systematically verify the output sequence for numbers 1-16, ensuring correct ‘Fizz’, ‘Buzz’, and ‘FizzBuzz’ substitutions according to the classic rules.

Test Coverage Overview

The test suite provides thorough coverage of the FizzBuzz algorithm implementation:

  • Sequential testing from 1 to 16 with all key cases
  • Validation of Fizz (multiples of 3), Buzz (multiples of 5), and FizzBuzz (multiples of both)
  • Platform-independent output verification
  • Stream handling and output capture testing

Implementation Analysis

The testing approach utilizes Spring dependency injection and JUnit framework features:

  • Spring ApplicationContext for bean management
  • Custom output stream capture mechanism
  • Modular test structure with setUp/tearDown lifecycle methods
  • Platform-independent line separator handling

Technical Details

Testing infrastructure components:

  • JUnit 4 testing framework
  • Spring Framework for dependency injection
  • ByteArrayOutputStream for output capture
  • BufferedOutputStream for stream handling
  • TestConstants for centralized test data

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test lifecycle management with @Before and @After annotations
  • Resource cleanup and system state restoration
  • Centralized test constants
  • Cross-platform compatibility considerations
  • Clear test method organization and naming

enterprisequalitycoding/fizzbuzzenterpriseedition

src/test/java/FizzBuzzTest.java

            
import static org.junit.Assert.assertEquals;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.interfaces.FizzBuzz;

/**
 * Tests for FizzBuzz
 */
public class FizzBuzzTest {

	private PrintStream out;
	private FizzBuzz fb;

	/**
	 * @return void
	 */
	@Before
	public void setUp() {
		final ApplicationContext context = new ClassPathXmlApplicationContext(TestConstants.SPRING_XML);
		this.fb = (FizzBuzz) context.getBean(TestConstants.STANDARD_FIZZ_BUZZ);
		this.out = System.out;
		((ConfigurableApplicationContext) context).close();
	}

	/**
	 * @return void
	 */
	@After
	public void tearDown() {
		System.setOut(this.out);
	}

	/**
	 * @param n int
	 * @param s String
	 * @throws IOException
	 */
	private void doFizzBuzz(final int n, final String s) throws IOException {
		final ByteArrayOutputStream baos = new ByteArrayOutputStream();
		final BufferedOutputStream bos = new BufferedOutputStream(baos);
		System.setOut(new PrintStream(bos));

		this.fb.fizzBuzz(n);

		System.out.flush();
		String platformDependentExpectedResult = s.replaceAll("\\n", System.getProperty("line.separator"));
		assertEquals(platformDependentExpectedResult, baos.toString());
	}

	/**
	 * @throws IOException
	 * @return void
	 */
	@Test
	public void testFizzBuzz() throws IOException {
		this.doFizzBuzz(TestConstants.INT_1, TestConstants._1_);
		this.doFizzBuzz(TestConstants.INT_2, TestConstants._1_2_);
		this.doFizzBuzz(TestConstants.INT_3, TestConstants._1_2_FIZZ);
		this.doFizzBuzz(TestConstants.INT_4, TestConstants._1_2_FIZZ_4);
		this.doFizzBuzz(TestConstants.INT_5, TestConstants._1_2_FIZZ_4_BUZZ);
		this.doFizzBuzz(TestConstants.INT_6, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ);
		this.doFizzBuzz(TestConstants.INT_7, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7);
		this.doFizzBuzz(TestConstants.INT_8, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8);
		this.doFizzBuzz(TestConstants.INT_9, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ);
		this.doFizzBuzz(TestConstants.INT_10, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ);
		this.doFizzBuzz(TestConstants.INT_11, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11);
		this.doFizzBuzz(TestConstants.INT_12, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11_FIZZ);
		this.doFizzBuzz(TestConstants.INT_13, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11_FIZZ_13);
		this.doFizzBuzz(TestConstants.INT_14, TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11_FIZZ_13_14);
		this.doFizzBuzz(TestConstants.INT_15,
				TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11_FIZZ_13_14_FIZZ_BUZZ);
		this.doFizzBuzz(TestConstants.INT_16,
				TestConstants._1_2_FIZZ_4_BUZZ_FIZZ_7_8_FIZZ_BUZZ_11_FIZZ_13_14_FIZZ_BUZZ_16);
	}

}