
IntelliJ IDEA单元测试结果快速消失的解决方法
在使用IntelliJ IDEA进行Java单元测试时,经常遇到打印的调试信息一闪而过的情况。这是因为IDEA的控制台默认设置会自动滚动到顶部。
解决方法:取消IDEA控制台的自动滚动功能。 具体步骤如下:
- 打开IntelliJ IDEA的设置(Preferences)窗口 (File -> Settings 或快捷键 Ctrl+Alt+S)。
- 依次导航到:
Tools->Debugger->Console。 - 取消勾选“
Enable automatic scroll to the first line”复选框。 - 应用设置并关闭设置窗口。
再次运行单元测试,打印信息将保留在控制台中,直到手动关闭。
立即学习“Java免费学习笔记(深入)”;
以下是一个示例代码,用于演示如何打印测试结果:
import org.junit.Test;
enum Color {
RED, GREEN, BLUE
}
public class MyTest {
@Test
public void testEnum() {
Color[] colors = Color.values();
for (Color color : colors) {
System.out.println(color + " at index " + color.ordinal());
}
System.out.println(Color.valueOf("RED"));
}
}










