gpt4 book ai didi

java - 如何用 Java 编写 JUnit 测试,检查一个特定的打印,该打印依赖于循环中的用户输入

转载 作者:行者123 更新时间:2023-11-28 21:07:18 27 4
gpt4 key购买 nike

<分区>

我尝试在 IntelliJ 中为修改字符串的应用程序编写 JUnit 测试。现在如何在控制台上测试一个特定的打印?

假设起始字符串作为“Hello World”存储在 args[0] 中,并且我们有一个方法可以更改字符串中的单个字母。方法执行后会打印出字符串。

public class modifyString {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while(true) {
// Print the string
System.out.println(args[0]);

// Wait for a userinput that only contains two chars,
// e.g. '0h' <- 'Change the index 0 to 'h''
// (the order of the chars is unimportant)
String input = scanner.nextLine();

if(preconditionFullfilled(input)) {
executeOperation(input, args);
}
}
}
}

例如:在 args[0] 中带有“Hello World”字符串的输入“0h”的预期输出应该是“hello World”。

如何编写 JUnit 测试来测试这个特定结果?

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