12

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:

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.

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.

1

1 Answer 1

21

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:

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.

5
  • 2
    There is also a MockHttpServletRequest in spring-test if I remember well
    – user180100
    Jan 8, 2014 at 13:05
  • Thank you Luciano, this works well. I was going down the path of ByteArrayInputStream already but couldn't cast it to servletInputStream, and it just didn't occurred to me to wrap it the way you did. @RC yes there is a MockHttpServletRequest, but in the example above I wanted to have as less package dependency as possible because thats not my question so I kept it to using Mockito only.
    – Churk
    Jan 8, 2014 at 13:19
  • 3
    If you're using spring-test, the DelegatingServletInputStream class is already available from the framework (org.springframework.mock.web.DelegatingServletInputStream).
    – RMorrisey
    Sep 3, 2014 at 19:21
  • Nice... i have pinched your work and cited it in my code :) Dec 31, 2015 at 1:21
  • 1
    Most elegant solution with mockito framework IMHO is blog.timmattison.com/archives/2014/12/16/… - Thanks Tim Mattison!
    – dev_doctor
    Dec 20, 2017 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.