我正在使用 JUnit 测试我的 Java 程序。 该程序包括一些 JavaFX GUI 界面和其他日志记录代码。但是,在测试期间,我不希望对这些进行测试。有没有办法在测试和开发之间切换代码?
描述可以很抽象,我举个例子:
public class Helloworld {
/**
* @param args the command line arguments
*/
public static int greetingCnt = 0;
public static void main(String[] args) {
Helloworld helloword = new Helloworld();
helloword.greetWorld();
helloword.greetWorld();
helloword.greetWorld();
System.out.println("Greating count: " + greetingCnt);
}
public void greetWorld() {
System.out.println("Hello World!");
++greetingCnt;
//some other computation...
}
}
在这个例子中,如果我只想测试正确数量的 greetingCnt 但不想打印任何内容或执行任何额外的计算。但在实际程序执行过程中,对程序的功能没有任何影响。请问有什么办法吗?
谢谢!
最佳答案
如果没有您的额外努力,这是不可能的。
计算机根本无法判断哪个计算会影响什么。
您可以通过替换默认的 PrintWriter
来关闭打印到控制台等特定功能,但没有通用的解决方案。
关于java - 在 JUnit 测试期间禁用部分 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408784/