title: How to test Spring Rest Controller with Mockito.when if controller has HttpServletRequest injected in method
body: <p>My Controller looks like this</p>
  
  <pre><code>@GetMapping("/networks/{networkId}")
  public Resource&lt;Network&gt; getNetwork(
    @PathVariable("networkId") UUID id,
    HttpServletRequest request) {
  
      Resource&lt;Network&gt; network = networkClient.getNetwork(id);
  
      network.removeLinks();
      network.add(LinkHelper.getNetworkObjectLinkArray(request, id));
  
      return network;
  }
  </code></pre>
  
  <p>And test looks like so</p>
  
  <pre><code>@Test
  public void getNetwork() throws Exception {
      Resource&lt;Network&gt; networkResource = createSampleNetwork(organizationId, "Test Network 01");
  
      MockHttpServletRequest mockedRequest = new  MockHttpServletRequest(context.getServletContext()); 
      UUID networkId = HalParser.parseUUIDFromLink(networkResource.getId());
      when(networkRestController.getNetwork(networkId, mockedRequest))
          .thenReturn(networkResource);
  
      mockMvc.perform(get("/networks/" + HalParser.parseUUIDFromLink(networkResource.getId()))
          .accept(MediaType.APPLICATION_JSON))
          .andExpect(status().isOk())
          .andDo(print())
          .andDo(document.document(
                    responseFields(
                        fieldWithPath("name").description("The network's name"),
                        fieldWithPath("caName").description("The network's ca name"),
                        fieldWithPath("productFamily").description("The network's product family"),
                        fieldWithPath("productVersion").description("The version of the network core software"),
                        fieldWithPath("status").description("Provisioning status of the network"),
                        fieldWithPath("createdAt").description("Record creation time"),
                        fieldWithPath("updatedAt").description("Record last modification time"),
                        fieldWithPath("_links").description("&lt;&lt;resources-index-links,Links&gt;&gt; to other resources")
                    )
            ));
  }
  </code></pre>
  
  <p>The issue is I am getting the error </p>
  
  <pre><code>"Method threw 'org.mockito.exceptions.misusing.UnfinishedStubbingException' exception. Cannot evaluate MyRestController$$EnhancerByMockitoWithCGLIB$$6fa8dd17.toString()"
  </code></pre>
  
  <p>This works if I remove the HttpServletRequest from the controller method and test.</p>