How can I unit test a method that uses a Scanner and an InputStream?

So here is the code: public int foo(InputStream in) { int counter = 0; Scanner scanner = new Scanner(in); while(scanner.hasNextLine()) { counter++; scanner.nextLine(); } return counter; } Normally I will be passing a FileInputStream to this method, however I want to be able to test it without accessing a physical file. Should I mock the File object? How to

我如何测试使用Scanner和InputStream的方法?

所以这里是代码: public int foo(InputStream in) { int counter = 0; Scanner scanner = new Scanner(in); while(scanner.hasNextLine()) { counter++; scanner.nextLine(); } return counter; } 通常我会传递一个FileInputStream到这个方法,但是我希望能够在不访问物理文件的情况下测试它。 我应该嘲笑File对象吗? 如何实施 @Test public void shouldReturnThree(){ } 你可以

Make Scanner read a String in java

I am converting command line java application to swing GUI. I have a Parser class that reads input from Scanner Scanner in = new Scanner(System.in); command = in.nextInt(); Is it possible to pass String instead of (System.in) ? Or, is there a better way to deal? You can use nextLine() of Scanner class to read string value entered in console. Below is an example: public class Parser { p

让扫描器在java中读取一个字符串

我将命令行java应用程序转换为摆动GUI。 我有一个从Scanner读取输入的解析器类 Scanner in = new Scanner(System.in); command = in.nextInt(); 是否可以传递String而不是(System.in)? 或者,有没有更好的方法来处理? 您可以使用Scanner类的nextLine()读取在控制台中输入的字符串值。 下面是一个例子: public class Parser { public static void main(String[] args) { Scanner in = new Scanner(System.in);

How to convert a String[] array to InputStream in Java

I have a String[] array and I need to convert it to InputStream . I've seen Byte[] -> InputStream and String -> InputStream , but not this. Any tips? You can construct a merged String with some separator and then to byte[] and then to ByteArrayInputStream . Here's a way to convert a String to InputStream in Java. Have a look at the following link: http://www.mkyong.com/java

如何在Java中将String []数组转换为InputStream

我有一个String[]数组,我需要将其转换为InputStream 。 我见过Byte[] - > InputStream和String - > InputStream ,但没有。 有小费吗? 您可以使用某个分隔符构造一个合并的String ,然后构造byte[] ,然后ByteArrayInputStream 。 这里有一种将String转换为InputStream的方法。 看看下面的链接:http://www.mkyong.com/java/how-to-convert-string-to-inputstream-in-java/ 但是,代码的区别在于,您应该在转

How can I read a String into an inputStream in Java?

Possible Duplicate: How do I convert a String to an InputStream in Java? How can I read a String into an InputStream in Java ? I want to be able to convert String say = "say" into an InputStream/InputSource. How do I do that? public class StringToInputStreamExample { public static void main(String[] args) throws IOException { String str = "This is a String ~ GoGoGo";

如何在Java中将字符串读入inputStream?

可能重复: 如何在Java中将字符串转换为InputStream? 我如何在Java中将一个字符串读入InputStream? 我希望能够将String say = "say"转换为InputStream / InputSource。 我怎么做? public class StringToInputStreamExample { public static void main(String[] args) throws IOException { String str = "This is a String ~ GoGoGo"; // convert String into InputStream InputStream is

Easy way to write contents of a Java InputStream to an OutputStream

I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. Obviously, the byte buffer code isn't difficult to write, but I suspect I'm just missing something which would make my life easier (and the code clearer). So, given an InputStream in and an OutputStream out , is there a simpler way to write the

简单的方法将Java InputStream的内容写入OutputStream

今天我惊讶地发现,我无法找到任何简单的方法将InputStream的内容写入到Java中的OutputStream中。 显然,字节缓冲区代码不难写,但我怀疑我只是错过了一些可以让我的生活更轻松(代码更清晰)的东西。 所以,如果InputStream in和OutputStream out ,是否有更简单的方法来编写以下内容? byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len != -1) { out.write(buffer, 0, len); len = in.read(

Unable to intercept and manipulate HttpServletResponse in Spring Boot

I have a requirement to Base64 decode every JSON request payload that my Spring Boot service receives. The JSON payload would have been Base64 encoded at the client before posting using the HTTP POST method. Further, I also need to Base64 encode the JSON response before presenting to the calling client application. I am required to reduce boilerplate code by using handler interceptors. I hav

无法拦截和操作Spring Boot中的HttpServletResponse

我需要Base64解码我的Spring Boot服务收到的每个JSON请求负载。 在使用HTTP POST方法发布之前,JSON负载将在客户端上进行Base64编码。 此外,我还需要Base64对提交给调用客户端应用程序之前的JSON响应进行编码。 我需要通过使用处理程序拦截器来减少样板代码。 我已经通过使用拦截器实现了操作的请求/传入段,但尚未实现响应段的操作。 我已经发布了下面的代码片段。 拦截响应的代码和base64对它的编码是在拦截器类的post

elasticsearch: convert StreamOutput to String

I'm overriding FilterClient so I can see incoming requests. I'd like some way to get a String representation of the ActionRequest that's passed in. ActionRequest let's you write to a StreamOuput , which is an Elasticsearch type that is a subclass of OutputStream . This SO post shows how to convert OutputStream to a String, but I'm forced to use StreamOuput due to the FilterC

elasticsearch:将StreamOutput转换为String

我重写FilterClient以便可以看到传入的请求。 我想办法让一个String中的代表性ActionRequest这是一个在过去。 ActionRequest让你写一个的StreamOuput ,这是一个Elasticsearch类型是子类OutputStream 。 这个SO帖子展示了如何将OutputStream转换为字符串,但由于FilterClient API,我不得不使用StreamOuput 。 我如何获得ActionRequest的字符串表示或至少一个可读的版本,它会显示有关请求的有用信息? (调用ActionRequest

Java interop: pass an OutputStream argument and get back a String

I'm using a java library and want to call a method which exports data. This method has two variants: one with a String parameter which is used as a destination file name, and another which accepts an OutputStream object and writes to it with an OutputStreamWriter. I'd like to use the second method and be able to get back the OutputStream into a String so I can do whatever I want with

Java interop:传递一个OutputStream参数并获取一个String

我正在使用一个Java库,并且想调用一个导出数据的方法。 此方法有两种变体:一种具有用作目标文件名的字符串参数,另一种接受OutputStream对象并使用OutputStreamWriter对其进行写入。 我想使用第二种方法,并且能够将OutputStream取回到String中,这样我就可以随心所欲地执行任何操作。 我不确定它实际上是可能的,但是当我发现clojure.core / with-out-str我有一些希望,但是我不知道如何使用它。 是否有可能从Clojure

stream of a Process returned by Runtime.exec()

How do I print to stdout the output string obtained executing a command? So Runtime.getRuntime().exec() would return a Process , and by calling getOutputStream() , I can obtain the object as follows, but how do I display the content of it to stdout? OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream(); Thanks I believe you are trying to get the output from proces

由Runtime.exec返回的进程的流()

如何打印输出字符串以执行命令? 因此, Runtime.getRuntime().exec()会返回一个Process ,并且通过调用getOutputStream() ,我可以获得如下的对象,但是如何将它的内容显示到stdout? OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream(); 谢谢 我相信你正在尝试从进程获取输出,为此你应该得到InputStream 。 InputStream is = Runtime.getRuntime().exec("ls").getInputStream(); Input

Test java programs that read from stdin and write to stdout

I am writing some code for a programming contest in java. The input to the program is given using stdin and output is on stdout. How are you folks testing programs that work on stdin/stdout? This is what I am thinking: Since System.in is of type InputStream and System.out is of type PrintStream, I wrote my code in a func with this prototype: void printAverage(InputStream in, PrintStream out

测试从stdin读取并写入标准输出的java程序

我正在编写一些Java编程竞赛的代码。 程序的输入使用stdin输出,输出在stdout上。 你如何测试在stdin / stdout上工作的程序? 这就是我的想法: 由于System.in是InputStream类型,System.out是PrintStream类型,因此我使用此原型在func中编写了代码: void printAverage(InputStream in, PrintStream out) 现在,我想用junit来测试它。 我想使用一个字符串伪造System.in,并接收字符串中的输出。 @Test void testPrintAv