I have been using have for two years now and now I'm trying to learn TDD (Test-Driven Development) and JUnit, the FizzBuzz Kata looked like a good place to start. So I did it:
public class FizzBuzz {
public static void main(String[] args) {
Generator generator = new Generator();
List<String> numbers = generator.fill(1, 100);
numbers.forEach( item -> System.out.println(item) );
}
}
It's works find by now I have a question regarding my GeneratorTest.java
:
public class GeneratorTest {
@Test
public void checkFizz() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
final int fizzNumber = 3;
final int containsFizz = 13;
final String result1 = generator.transform(fizzNumber);
final String result2 = generator.transform(containsFizz);
// Assert
assertEquals("Fizz", result1);
assertEquals("Fizz", result2);
}
@Test
public void checkBuzz() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
final int buzzNumber = 5;
final int containsBuzz = 52;
final String result1 = generator.transform(buzzNumber);
final String result2 = generator.transform(containsBuzz);
// Assert
assertEquals("Buzz", result1);
assertEquals("Buzz", result2);
}
@Test
public void checkFizzBuzz() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
final int fizzBuzzNumber = 15;
final int containsFizzBuzz = 53;
final String result1 = generator.transform(fizzBuzzNumber);
final String result2 = generator.transform(containsFizzBuzz);
// Assert
assertEquals("FizzBuzz", result1);
assertEquals("FizzBuzz", result2);
}
@Test
public void checkFillSize() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
final int lowerBound = 2;
final int upperBound = 100;
final int expectedSize = upperBound - lowerBound + 1;
List<String> filledList = generator.fill(lowerBound, upperBound);
//Assert
assertEquals(expectedSize, filledList.size() );
}
@Test
public void checkFillContents() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
ArrayList<String> expectedList = new ArrayList<>( Arrays.asList("1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "Fizz", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "Fizz", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz") ) ;
List<String> filledList = generator.fill(1, expectedList.size() );
//Assert
assertEquals(expectedList, filledList);
}
@Test
public void checkFillCrossedBounds() {
//Arrange
Generator generator = new Generator(); // This line is on every test!
//Act
final int lowerBound = 100;
final int upperBound = 1;
final int expectedSize = 0;
List<String> filledList = generator.fill(lowerBound, upperBound);
//Assert
assertEquals(expectedSize, filledList.size() );
}
Is there a way to make a singleton generator
? And if so, is it a good practice?
I thought of doing it in the constructor but, I'm not sure if is the "right" way because I'm fairly new to JUnit and TDD.
PS: Greetings from Spain!