Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
测试从stdin读取并写入stdout的java程序_Java_Junit_Mocking - Fatal编程技术网

测试从stdin读取并写入stdout的java程序

测试从stdin读取并写入stdout的java程序,java,junit,mocking,Java,Junit,Mocking,我正在为java编程竞赛写一些代码。程序的输入使用标准输入,输出在标准输出上。你们是如何测试在stdin/stdout上工作的程序的?这就是我的想法: 由于System.in的类型为InputStream,System.out的类型为PrintStream,因此我使用以下原型在func中编写了代码: void printAverage(InputStream in, PrintStream out) 现在,我想用junit测试一下。我想在使用字符串时伪造系统,并以字符串形式接收输出 @Test

我正在为java编程竞赛写一些代码。程序的输入使用标准输入,输出在标准输出上。你们是如何测试在stdin/stdout上工作的程序的?这就是我的想法:

由于System.in的类型为InputStream,System.out的类型为PrintStream,因此我使用以下原型在func中编写了代码:

void printAverage(InputStream in, PrintStream out)
现在,我想用junit测试一下。我想在使用字符串时伪造系统,并以字符串形式接收输出

@Test
void testPrintAverage() {

    String input="10 20 30";
    String expectedOutput="20";

    InputStream in = getInputStreamFromString(input);
    PrintStream out = getPrintStreamForString();

    printAverage(in, out);

    assertEquals(expectedOutput, out.toString());
}
实现getInputStreamFromString()和getPrintStreamForString()的“正确”方法是什么


我是不是把这个问题弄得比需要的更复杂了?

编辑:对不起,我误读了你的问题

使用扫描仪或bufferedreader读取,后者比前者快得多

Scanner jin = new Scanner(System.in);

BufferedReader reader = new BufferedReader(System.in);
使用print writer向标准输出写入。您也可以直接打印到Syso,但速度较慢

System.out.println("Sample");
System.out.printf("%.2f",5.123);

PrintWriter out = new PrintWriter(System.out);
out.print("Sample");
out.close();
请尝试以下操作:

String string = "aaa";
InputStream stringStream = new java.io.ByteArrayInputStream(string.getBytes())
PipedInputStream pipeIn = new PipedInputStream(1024);
System.setIn(pipeIn);

PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);

// then I can write to the pipe
pipeOut.write(new byte[] { ... });

// if I need a writer I do:
Writer writer = OutputStreamWriter(pipeOut);
writer.write("some string");

// call code that reads from System.in
processInput();
stringStream
是一个从输入字符串读取字符的流

OutputStream outputStream = new java.io.ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
// .. writes to printWriter and flush() at the end.
String result = outputStream.toString()
printStream
是一个
printStream
,它将写入
outputStream
,而后者将能够返回字符串

我正在为java编程竞赛写一些代码。程序的输入使用标准输入,输出在标准输出上。你们是如何测试在stdin/stdout上工作的程序的

另一种向
系统发送字符的方法是使用
PipedInputStream
PipedOutputStream
。可能是以下情况:

String string = "aaa";
InputStream stringStream = new java.io.ByteArrayInputStream(string.getBytes())
PipedInputStream pipeIn = new PipedInputStream(1024);
System.setIn(pipeIn);

PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);

// then I can write to the pipe
pipeOut.write(new byte[] { ... });

// if I need a writer I do:
Writer writer = OutputStreamWriter(pipeOut);
writer.write("some string");

// call code that reads from System.in
processInput();
另一方面,正如@Mihai Toader所提到的,如果我需要测试
System.out
,那么我会做如下操作:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));

// call code that prints to System.out
printSomeOutput();

// now interrogate the byte[] inside of baos
byte[] outputBytes = baos.toByteArray();
// if I need it as a string I do
String outputStr = baos.toString();

Assert.assertTrue(outputStr.contains("some important output"));

也许,并且可以帮助…可能的重复您是指PrintStream而不是PrintWriter吗?是的。我一开始误解了这个问题,因为需要一个printWriter,你不能将
System.in
传递到一个BufferedReader中。您需要首先将其包装在
InputStreamReader
中。