java.lang.IllegalArgumentException

tip

If you're unit testing, you should instantiate only the service and mock the dependencies. If you're testing integration, you should add all the mocking to the configurations file.

You have a different solution? A short tip here would help you and many other users who saw this issue last week.

  • Yet another filter, and unit test failed with exception {noformat} java.lang.IllegalArgumentException: No InputStream specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.util.StreamUtils.copy(StreamUtils.java:118) at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:56) at com.pb.ivrcgate.util.filter.HttpRequestWrapper.<init>(HttpRequestWrapper.java:27) at com.pb.ivrcgate.util.filter.HttpDumperFilter.doFilterInternal(HttpDumperFilter.java:25) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137) at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141) at com.pb.ivrcgate.controller.GateControllerTest.badUri(GateControllerTest.java:40) {noformat} Filter code fragment: {code} public class HttpDumperFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { try { HttpRequestWrapper request = new HttpRequestWrapper(httpServletRequest); HttpResponseWrapper response = new HttpResponseWrapper(httpServletResponse); filterChain.doFilter(request, response); // ... } catch (Exception ex) { logger.error("", ex); throw ex; } } // ... } {code} {{HttpRequestWrapper}} code fragment: {code} public class HttpRequestWrapper extends HttpServletRequestWrapper { private final byte[] content; public HttpRequestWrapper(HttpServletRequest request) throws IOException { super(request); this.content = StreamUtils.copyToByteArray(request.getInputStream()); // line #27 } // ... } {code} In compliance with Servlet API and documentation - {{MockMvc}} must return object of class ServletInputStream, and can't return null. For example, tomcat return ServletInputStream object even for GET requests, and never return null from getInputStream() method. Looks like this is bug in {{MockMvc}}.
    via by Gena Makhomed,
  • Yet another filter, and unit test failed with exception {noformat} java.lang.IllegalArgumentException: No InputStream specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.util.StreamUtils.copy(StreamUtils.java:118) at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:56) at com.pb.ivrcgate.util.filter.HttpRequestWrapper.<init>(HttpRequestWrapper.java:27) at com.pb.ivrcgate.util.filter.HttpDumperFilter.doFilterInternal(HttpDumperFilter.java:25) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137) at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141) at com.pb.ivrcgate.controller.GateControllerTest.badUri(GateControllerTest.java:40) {noformat} Filter code fragment: {code} public class HttpDumperFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { try { HttpRequestWrapper request = new HttpRequestWrapper(httpServletRequest); HttpResponseWrapper response = new HttpResponseWrapper(httpServletResponse); filterChain.doFilter(request, response); // ... } catch (Exception ex) { logger.error("", ex); throw ex; } } // ... } {code} {{HttpRequestWrapper}} code fragment: {code} public class HttpRequestWrapper extends HttpServletRequestWrapper { private final byte[] content; public HttpRequestWrapper(HttpServletRequest request) throws IOException { super(request); this.content = StreamUtils.copyToByteArray(request.getInputStream()); // line #27 } // ... } {code} In compliance with Servlet API and documentation - {{MockMvc}} must return object of class ServletInputStream, and can't return null. For example, tomcat return ServletInputStream object even for GET requests, and never return null from getInputStream() method. Looks like this is bug in {{MockMvc}}.
    via by Gena Makhomed,
  • Spring Boot test class wont inject beans
    via Stack Overflow by rayman
    ,
  • Spring file upload by ajax
    via Stack Overflow by kris14an
    ,
    • java.lang.IllegalArgumentException: No InputStream specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.util.StreamUtils.copy(StreamUtils.java:118) at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:56) at com.pb.ivrcgate.util.filter.HttpRequestWrapper.<init>(HttpRequestWrapper.java:27) at com.pb.ivrcgate.util.filter.HttpDumperFilter.doFilterInternal(HttpDumperFilter.java:25) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137) at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141) at com.pb.ivrcgate.controller.GateControllerTest.badUri(GateControllerTest.java:40)

    Users with the same issue

    derprogger
    2 times, last one,
    tomthomas
    121 times, last one,
    tvrmsmith
    1 times, last one,
    osvzs
    2 times, last one,
    Unknown visitor1 times, last one,
    189 more bugmates