给定输入的JUnit输出模拟
在这里,我想在某个方法中使用给定输入的JUnit输出模拟,junit,system-rules,Junit,System Rules,在这里,我想在某个方法中使用System.out.println()语句检查输入add5我的输出应该是某个语句。可以模拟输出语句吗 您可以使用将输出写入System.out并对其进行断言: public void MyTest { @Rule public final TextFromStandardInputStream systemInMock = emptyStandardInputStream(); @Test public void shouldTakeUserInp
System.out.println()
语句检查输入add5我的输出应该是某个语句。可以模拟输出语句吗 您可以使用将输出写入System.out
并对其进行断言:
public void MyTest {
@Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
@Test
public void shouldTakeUserInput() {
systemInMock.provideLines("add 5", "another line");
InputOutput inputOutput = new InputOutput();
assertEquals("add 5", inputOutput.getInput());
}
}
myCode.doSomething()正在使用println(),当尝试通过getLog测试用例未通过时,如果我使用print方法,它将pass@SasirekaGanesan
println
在输出中添加换行符。如果使用它,则应针对“预期输出”
。
public class MyTest {
@Rule
public final TextFromStandardInputStream systemInMock =
emptyStandardInputStream();
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void shouldTakeUserInput() {
systemInMock.provideLines("add 5", "another line");
MyCode myCode = new MyCode();
myCode.doSomething();
assertEquals("expected output", systemOutRule.getLog());
}
}