我一直在努力测试 if else 和 for 循环,这是一个例子,你能不能指导我自动化测试如何实际适用于这些语句?
for (int i=1; i<=10; i++){ // for loop from 1 to 10
System.out.println(" guess "+i+ ":");
int guess = scan.nextInt();
//if guess is greater than number entered
if(guess>number)
System.out.println("Clue: lower");
//if guess is less than number entered
else if (guess<number )
System.out.println("Clue: Higher");
//if guess is equal than number entered
else if(guess==number) {
System.out.println("Correct answer after only "+ i + " guesses – Excellent!");
}
- 步骤 0 - 将代码重构为可行、可编译的示例
- 步骤 1 - 重构以便能够进行依赖注入
- 步骤 2 - 将 java.util.Scanner 包装到您自己的委托中
- 步骤 3 - 创建一个常规测试
- 步骤 4 - 从循环中提取处理单次尝试
- 步骤 5 - 为游戏创建单元测试
- 步骤 6 - 为单一猜测处理程序创建单元测试
- 结语
步骤 0 - 将代码重构为可行、可编译的示例
public class Main {
public static void main(String[] args) {
new Game().run();
}
}
public class Game {
public void run() {
Scanner scan = new Scanner(System.in);
int number = new Random().nextInt(100);
for (int i = 1; i <= 10; i++) {
System.out.println(" guess " + i + ":");
int guess = scan.nextInt();
if (guess > number) {
System.out.println("Clue: lower");
} else if (guess < number) {
System.out.println("Clue: Higher");
} else if (guess == number) {
System.out.println("Correct answer after only " + i + " guesses – Excellent!");
}
}
}
}
步骤 1 - 重构以便能够进行依赖注入
所有这些操作都应该通过 IDE 重构来完成 - 将方法、提取到字段、创建构造函数、将初始值设定项移动到构造函数、提取到参数等。
public class Main {
public static void main(String[] args) {
Game game = new Game(new Scanner(System.in), System.out, new Random().nextInt(100));
game.run();
}
}
public class Game {
private final Scanner scanner;
private final PrintStream printStream;
private final int number;
public Game(Scanner scanner, PrintStream out, int number) {
this.scanner = scanner;
this.printStream = out;
this.number = number;
}
public void run() {
for (int i = 1; i <= 10; i++) {
printStream.println(" guess " + i + ":");
int guess = scanner.nextInt();
if (guess > number) {
printStream.println("Clue: lower");
} else if (guess < number) {
printStream.println("Clue: Higher");
} else if (guess == number) {
printStream.println("Correct answer after only " + i + " guesses – Excellent!");
}
}
}
}
步骤 2 - 将 java.util.Scanner 包装到您自己的委托中
试图模拟java.util.Scanner
你很快就会发现类是最终的,所以我们创建了一个简单的包装器,并在Main
和Game
类中用我们的包装器替换java.util.Scanner
。
public class MyScanner {
private final Scanner scanner = new Scanner(System.in);
public int nextInt() {
return scanner.nextInt();
}
}
步骤 3 - 创建一个常规测试
import org.junit.Test;
import org.mockito.InOrder;
import java.io.PrintStream;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class GameTest {
private final MyScanner scanner = mock(MyScanner.class);
private final PrintStream printStream = mock(PrintStream.class);
private final Game game = new Game(scanner, printStream, 15);
@Test
public void returnsCorrectOutputWhenNumberGuessedAfterThreeAttempts() {
when(scanner.nextInt()).thenReturn(10, 20, 15);
game.run();
InOrder inOrder = inOrder(scanner, printStream);
inOrder.verify(printStream).println(" guess 1:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: Higher");
inOrder.verify(printStream).println(" guess 2:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: lower");
inOrder.verify(printStream).println(" guess 3:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Correct answer after only 3 guesses – Excellent!");
}
}
步骤 4 - 从循环中提取处理单次尝试
在循环中,有 3 个可能的流,我们有 10 个迭代。这总共给出了 3^10 ~ 60.000 个可能的流。而且我们不想写6万个测试。因此,我们再次重构(仅使用 IDE 重构选项(以达到以下状态。请注意,所有内容都应编译,并且测试仍应为绿色。
public class Main {
public static void main(String[] args) {
Game game = new Game(new SingleGuessHandler(new MyScanner(), System.out), new Random().nextInt(100));
game.run();
}
}
public class Game {
private final SingleGuessHandler singleGuessHandler;
private final int number;
public Game(SingleGuessHandler singleGuessHandler, int number) {
this.singleGuessHandler = singleGuessHandler;
this.number = number;
}
public void run() {
for (int i = 1; i <= 10; i++) {
singleGuessHandler.runSingleGuess(i, number);
}
}
}
public class SingleGuessHandler {
private final MyScanner scanner;
private final PrintStream printStream;
public SingleGuessHandler(MyScanner scanner, PrintStream printStream) {
this.scanner = scanner;
this.printStream = printStream;
}
public void runSingleGuess(int attempt, int number) {
printStream.println(" guess " + attempt + ":");
int guess = scanner.nextInt();
if (guess > number) {
printStream.println("Clue: lower");
} else if (guess < number) {
printStream.println("Clue: Higher");
} else if (guess == number) {
printStream.println("Correct answer after only " + attempt + " guesses – Excellent!");
}
}
}
public class MyScanner {
private final Scanner scanner = new Scanner(System.in);
public int nextInt() {
return scanner.nextInt();
}
}
public class GameTest {
private final MyScanner scanner = mock(MyScanner.class);
private final PrintStream printStream = mock(PrintStream.class);
private final Game game = new Game(new SingleGuessHandler(scanner, printStream), 15);
@Test
public void returnsCorrectOutputWhenNumberGuessedAfterThreeAttempts() {
when(scanner.nextInt()).thenReturn(10, 20, 15);
game.run();
InOrder inOrder = inOrder(scanner, printStream);
inOrder.verify(printStream).println(" guess 1:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: Higher");
inOrder.verify(printStream).println(" guess 2:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: lower");
inOrder.verify(printStream).println(" guess 3:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Correct answer after only 3 guesses – Excellent!");
}
}
步骤 5 - 为游戏创建单元测试
我们现有的GameTest
一起测试几个类,但现在我们只想测试Game
和SingleGuessHandler
的相互作用。在这一点上,我已经意识到您的原始代码不包含任何逻辑来处理在正确猜测的情况下中断循环。这使得下面的单元测试更简单,否则runSingleGuess
将不得不返回 true/false 值(指示是否再次调用它(,并且我们将不得不再编写 1 个测试来检查它是否返回 false
,那么它不会再次调用。简而言之:
- 1 次故障测试 - 循环迭代 10 次
- 1 次成功测试 - 循环提前终止(我不会这样做,因为它不在您的原始代码中(
@Test
public void callsSingleGuessHandlerTenTimes() {
SingleGuessHandler singleGuessHandler = mock(SingleGuessHandler.class);
Game game = new Game(singleGuessHandler, 17);
game.run();
InOrder inOrder = inOrder(singleGuessHandler);
inOrder.verify(singleGuessHandler).runSingleGuess(1, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(2, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(3, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(4, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(5, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(6, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(7, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(8, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(9, 17);
inOrder.verify(singleGuessHandler).runSingleGuess(10, 17);
inOrder.verifyNoMoreInteractions();
}
步骤 6 - 为单一猜测处理程序创建单元测试
public class SingleGuessHandlerTest {
private final MyScanner scanner = mock(MyScanner.class);
private final PrintStream printStream = mock(PrintStream.class);
private final SingleGuessHandler singleGuessHandler = new SingleGuessHandler(scanner, printStream);
@Test
public void printsLowerClue() {
when(scanner.nextInt()).thenReturn(5);
singleGuessHandler.runSingleGuess(99, 4);
InOrder inOrder = inOrder(scanner, printStream);
inOrder.verify(printStream).println(" guess 99:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: lower");
inOrder.verifyNoMoreInteractions();
}
@Test
public void printsHigherClue() {
when(scanner.nextInt()).thenReturn(16);
singleGuessHandler.runSingleGuess(2, 100);
InOrder inOrder = inOrder(scanner, printStream);
inOrder.verify(printStream).println(" guess 2:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Clue: Higher");
inOrder.verifyNoMoreInteractions();
}
@Test
public void printsSuccessfulGuessMessage() {
when(scanner.nextInt()).thenReturn(65);
singleGuessHandler.runSingleGuess(8, 65);
InOrder inOrder = inOrder(scanner, printStream);
inOrder.verify(printStream).println(" guess 8:");
inOrder.verify(scanner).nextInt();
inOrder.verify(printStream).println("Correct answer after only 8 guesses – Excellent!");
inOrder.verifyNoMoreInteractions();
}
}
结语
请注意,课程MyScanner
和Main
未经测试。测试它们需要使用真实的java.util.Scanner
和真实的System.out
打印流。这是一种验收测试(一切都是真实的,没有假货或模拟(,在这种情况下,通过将 java 应用程序作为单独的进程运行并向其提供数字并检查其输出的内容,可能是最好的实现。
最好测试正在猜测的数字的范围。我会通过将new Random().nextInt(100)
提取到一个类中来做到这一点,例如 RandomNumberProvider
.然后剩下的就是:
- 将其作为依赖项添加到
Game
- 为
Game
传递Game
添加测试,以SingleGuessHandler
任何提供程序返回 - 为
RandomNumberProvider
添加测试 - 属性测试或通过模拟Random
另请注意,当不同的测试调用同一方法时,将使用不同的值。它可以防止在实现中意外硬编码这些值。
提取一个捕获代码逻辑的"纯函数",那么测试将是微不足道的。
static String reply(int guess, int number, int guessCount) {
return guess > number ? "Clue: lower"
: guess < number ? "Clue: Higher"
: "Correct answer after only " + guessCount + " guesses -- Excellent!";
}
guess()
提取函数并传递guess
和number
。让它返回结果字符串或表示它的抽象值。
将函数和计数器封装在新类中。每次guess()
时,递增它。当您达到 10 个猜测时禁用猜测。
现在,您可以控制代码的输入和输出,并且可以正确测试它,而不必依赖副作用(控制台输入/输出(。
你只需编写三个满足不同条件的不同断言。您还需要将其放入某种功能中。我从未尝试过对主函数进行单元测试,但我想你可以这样做,但通常它是在一个返回一些值的函数中。我不确定您将如何测试打印语句。也许您可以将文本放在变量中,然后打印出来。它看起来像这样。
public string gameFunction(int guess){
String printVariable;
for (int i=1; i<=10; i++){ // for loop from 1 to 10
System.out.println(" guess "+i+ ":");
int guess = scan.nextInt();
//if guess is greater than number entered
if(guess>number)
printVariable = "Clue: lower";
System.out.println("Clue: lower");
//if guess is less than number entered
else if (guess<number )
printVariable = "Clue: Higher";
System.out.println("Clue: Higher");
//if guess is equal than number entered
else if(guess==number) {
printVariable = "Correct answer after only "+ i + " guesses – Excellent!";
System.out.println("Correct answer after only "+ i + " guesses – Excellent!");
return printVariable;
}
}
@Test
public void testDiscreteize(){
int guess = 1
int number = 2
String testPrint = "Clue: lower";
String returnValue = game function(guess);
assertEquals(testPrint, returnValue);
}
请注意,我还没有测试过任何这些代码,这只是关于如何设置的一般想法。