如何模拟 javax.servlet.ServletInputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20995874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:49:31  来源:igfitidea点击:

How to Mock a javax.servlet.ServletInputStream

javaunit-testingservletsinputstreammockito

提问by Churk

I am creating some unit testing and trying to Mock out some calls. Here is what I have in my working code:

我正在创建一些单元测试并尝试模拟一些调用。这是我的工作代码中的内容:

String soapRequest = (SimUtil.readInputStream(request.getInputStream())).toString();
if (soapRequest.equals("My String")) { ... }

and SimUtil.readInputSteam looks like this:

SimUtil.readInputSteam 看起来像这样:

StringBuffer sb = new StringBuffer();
BufferedReader reader = null;
try  {
    reader = new BufferedReader(new InputStreamReader(inputStream));
    final int buffSize = 1024;
    char[] buf = new char[buffSize];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        sb.append(readData);
        buf = new char[buffSize];
    }
} catch (IOException e) {
    LOG.error(e.getMessage(), e);
} finally {
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

What I am trying to do is the request.getInputStream(), the stream returns certain String.

我想要做的是 request.getInputStream(),流返回某些字符串。

HttpServletRequest request = mock(HttpServletRequest.class);
ServletInputStream inputStream = mock(ServletInputStream.class);
when(request.getInputStream()).thenReturn(inputStream);

So This is the code I want to condition

所以这是我想要条件的代码

when(inputStream.read()).thenReturn("My String".toInt());

Any Help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by Luciano

Don't mock the InputStream. Instead, transform the String to an array of bytes using the getBytes()method. Then create a ByteArrayInputStream with the array as input, so that it returns the String when consumed, each byte at a time. Next, create a ServletInputStream that wraps a regular InputStream like the one from Spring:

不要嘲笑 InputStream。相反,使用getBytes()方法将 String 转换为字节数组 。然后使用数组作为输入创建一个 ByteArrayInputStream,以便它在使用时返回字符串,一次每个字节。接下来,创建一个 ServletInputStream 来包装一个常规的 InputStream,就像 Spring 中的那个一样:

public class DelegatingServletInputStream extends ServletInputStream {

    private final InputStream sourceStream;


    /**
     * Create a DelegatingServletInputStream for the given source stream.
     * @param sourceStream the source stream (never <code>null</code>)
     */
    public DelegatingServletInputStream(InputStream sourceStream) {
        Assert.notNull(sourceStream, "Source InputStream must not be null");
        this.sourceStream = sourceStream;
    }

    /**
     * Return the underlying source stream (never <code>null</code>).
     */
    public final InputStream getSourceStream() {
        return this.sourceStream;
    }


    public int read() throws IOException {
        return this.sourceStream.read();
    }

    public void close() throws IOException {
        super.close();
        this.sourceStream.close();
    }

}

and finally, the HttpServletRequest mock would return this DelegatingServletInputStream object.

最后,HttpServletRequest 模拟将返回这个 DelegatingServletInputStream 对象。