How to compile javax.servlet.ServletInputStream

I am creating some unit testing and trying to make 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'm trying to do is request.getInputStream (), the stream returns a specific string.

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

So this is the code I want to set

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

Any help would be greatly appreciated.

+6
source share
2 answers

Do not scoff at InputStream. Instead, convert String to an array of bytes using the getBytes () method . Then create a ByteArrayInputStream with an array as input so that it returns a string when consumed, each byte at a time. Then 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 mock file HttpServletRequest will return this DelegatingServletInputStream object.

+10
source

Like the accepted answer, but combined the idea from this question ...

 import org.apache.commons.io.IOUtils class MockServletInputStream extends ServletInputStream { InputStream inputStream MockServletInputStream(String string) { this.inputStream = IOUtils.toInputStream(string) } @Override int read() throws IOException { return inputStream.read() } } 
+2
source

All Articles