"Singleton" on JUnit test

时间:2017-09-20 18:49:27

标签: java junit tdd

I have been using have for two years now and now I'm trying to learn TDD (Test-Driven Development) and JUnit, the 看起来像是一个好的起点。所以我做到了:

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) );
    }
}

现在找到它的作品我对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() );
}

有没有办法制作单身generator?如果是这样,这是一个好习惯吗?

我想在构造函数中做到这一点但是,我不确定是否是&#34;对&#34;因为我对JUnit和TDD来说相当新。

PS:来自西班牙的问候!

1 个答案:

答案 0 :(得分:3)

我同意JB Nizet,每个测试都是彼此独立的。

所以,你的班级生成器不能是单身人士 我不太了解Java,但在每个Test Runner中你都有一些方法,如:

  • BeforeEach
  • AfterEach
  • BeforeAll
  • AfterAll

例如,您的第Generator generator = new Generator();行可以是:

public class GeneratorTest {
    private Generator generator = null

    @Before
    public void runBeforeEachMethod() {
       this.generator = new Generator();
    }

    // your tests
}

因此,对于每个测试,您都有Generator的新实例。 您也可以使用BeforeAll,但更改生成器的状态可能会导致一些不良副作用,从而导致测试失败。

您可以查看此文档:https://www.mkyong.com/unittest/junit-4-tutorial-1-basic-usage/