gpt4 book ai didi

java - 如何测试 System.out.println();通过 mock

转载 作者:行者123 更新时间:2023-11-29 06:31:09 25 4
gpt4 key购买 nike

<分区>

你好我必须练习如何使用 Mockito 有人可以告诉我我们如何使用模拟对象来测试基于控制台的输出测试例如

Random rand = new Random();
int number = 1+rand.nextInt(100); // random number 1 to 100
Scanner scan = new Scanner(System.in);

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("lue: Higher");
//if guess is equal than number entered
else if(guess==number) {
System.out.println("Correct answer after only "+ i + " guesses – Excellent!");
scan.close();
System.exit(-1);
}

}

System.out.println("you lost" + number);
scan.close();

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