gpt4 book ai didi

java - JUnit 测试上的 "Singleton"

转载 作者:行者123 更新时间:2023-12-01 20:13:14 27 4
gpt4 key购买 nike

我已经使用了两年了,现在我正在尝试学习 TDD(测试驱动开发)和 JUnit,FizzBuzz Kata看起来是一个不错的起点。所以我做到了:

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

有没有办法制作一个单例生成器?如果是这样,这是一个好的做法吗?

我想过在构造函数中执行此操作,但是我不确定这是否是“正确”的方法,因为我对 JUnit 和 TDD 还很陌生。

PS:来自西类牙的问候!

最佳答案

我同意 JB Nizet 的观点,每个测试都是相互独立的。

因此,您的类生成器一定不能是单例。
我不太了解 Java,但是在每个测试运行器中都有一些方法,例如:

  • 每个之前
  • 每次之后
  • 之前
  • 毕竟

例如,您的行 Generatorgenerator = 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/

关于java - JUnit 测试上的 "Singleton",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com