我是 jUnit 的新手,我正在运行程序运行时的输出测试。
我的 jUnit 测试是:
class Tests {
private final ByteArrayOutputStream outContent = new
ByteArrayOutputStream();
@BeforeEach
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@AfterEach
public void cleanUpStreams() {
System.setOut(null);
}
@Test
public void Test1() {
Mobile ios = new Mobile();
ByteArrayInputStream inContent = new
ByteArrayInputStream("".getBytes());
System.setIn(inContent);
Mobile.main(new String[0]);
System.setIn(System.in);
assertNotEquals("Welcome!"+System.lineSeparator(),outContent.toString());
}
}
我的部分程序是:
public class MobileApp {
public static void main(String[] args)
{
System.out.println("Welcome!");
}
}
我希望测试能够通过,因为输出应该是相同的。但是,在运行测试后,它失败了,因为它显示实际上没有输出任何内容。我不确定为什么会这样,因此不胜感激。 Result Comparison after run of test
最佳答案
我想这可能是由于 PrintStream
中的缓冲而发生的。您可以为 autoFlush parameter 传递 true
在构造 PrintStream
来解决问题时,例如
@BeforeEach
public void setUpStreams() {
System.setOut(new PrintStream(outContent, true));
}
关于java - jUnit 测试不输出任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445738/