title: JUnit Controller Testing on HttpServletRequest and HttpServletResponse
body: <p>I have this function and I would like to add the test, but have no idea on how to fill up the <code>HttpServletRequest</code> and <code>HttpServletResponse</code>'s part.</p>
  
  <pre><code>public void convertXlsToPdf(HttpServletDtoRequest httpServletDtoRequest) throws Exception {
      HttpServletRequest request = httpServletDtoRequest.getRequest();
      HttpServletResponse response = httpServletDtoRequest.getResponse();
  
      byte[] result =
          documentService.convertBinaryExcelToPdf(IOUtils.toByteArray(request.getInputStream()));
      ServletOutputStream servletOutputStream = response.getOutputStream();
  
      response.setContentType("application/pdf");
      response.setHeader("Content-Disposition",
          "attachment; filename=fpdf" + System.currentTimeMillis() + ".pdf");
      response.setContentLength(result.length);
  
      servletOutputStream.write(result, 0, result.length);
      servletOutputStream.flush();
      servletOutputStream.close();
  }
  </code></pre>
  
  <p>I have tried on something like this but it's failed and returned an <code>IllegalStateException</code>.</p>
  
  <pre><code>@Test
  public void convertXlsToPdf_Test() throws Exception {
      MockHttpServletRequest request = new MockHttpServletRequest();
      MockHttpServletResponse response = new MockHttpServletResponse();
  
      HttpServletDtoRequest httpServletDtoRequest = new HttpServletDtoRequest();
      httpServletDtoRequest.setRequest(request);
      httpServletDtoRequest.setResponse(response);
  
      Gson gson = new GsonBuilder().create(); //--&gt; ERROR HERE
      String json = gson.toJson(httpServletDtoRequest);
  
      this.mockMvc.perform(
          MockMvcRequestBuilders.get("PATH_TO_CLIENT")
              .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
              .content(json)).andExpect(status().isOk());
  
      verify(this.service).convertBinaryExcelToPdf(
          IOUtils.toByteArray(httpServletDtoRequest.getRequest().getInputStream()));
  }
  </code></pre>
  
  <p>The exception is :</p>
  
  <p>Caused by: com.g</p>
  
  <pre><code>oogle.gson.CircularReferenceException: circular reference error
          at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisitor.java:61)
          at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerializer(JsonSerializationVisitor.java:182)
          at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler(JsonSerializationVisitor.java:204)
          ... 72 more
  </code></pre>
  
  <p>How to do that? Thanks.</p>