Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定输入的JUnit输出模拟_Junit_System Rules - Fatal编程技术网

给定输入的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());
    }
}