title: how to test rest controller with custom spring DispatcherServlet
body: <p>I'm trying to test my controller and I want to my custom DispatcherServlet include on that.</p>
  <p>my dispatcher is something like this:</p>
  <pre><code> public class LoggableDispatcherServlet extends DispatcherServlet {
      private static final Logger LOGGER = LoggerFactory.getLogger(LoggableDispatcherServlet.class);
      @Autowired
      private LogsService logsService;
      @Override
      protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws 
       Exception 
      {
           .
           .
           .
           mapper.readTree(requestWrapper.getContentAsByteArray());
           int status = responseWrapper.getStatus();
           JsonNode newNode = mapper.readTree(responseWrapper.getContentAsByteArray());
           JsonNode responseJson= newNode;
           logsService.addLogs(request,response ,...)
  
  
      }
  }
  </code></pre>
  <p>and my test Class is something like this:</p>
  <pre><code>@SpringBootTest
  @RunWith(SpringRunner.class)
  public class RestControllerTest {
  
      private final Resource test1 = new ClassPathResource(&quot;test1.json&quot;);
  
      private MockMvc mockMvc;
  
      @Autowired
      private RestTemplate restTemplate;
  
      @Autowired
      private WebApplicationContext wac;
  
  
      @BeforeEach
      public void before() {
          mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
          this.mockRestServiceServer = MockRestServiceServer.bindTo(this.restTemplate)
                  .build()
  
      }
  
      @Test
      public void getAvailableLoanTest() throws Exception{
          String url1=&quot;http://localhost:80801/testService?param1=1&amp;param2=2&quot;;
  
          this.mockRestServiceServer
                  .expect(ExpectedCount.manyTimes(), requestTo(url1))
                  .andExpect(method(HttpMethod.GET))
                  .andRespond(withSuccess(this.test1, MediaType.APPLICATION_JSON_UTF8));
  
          this.mockMvc.perform(post(&quot;/api/myApi&quot;)
                  .contentType(MediaType.APPLICATION_JSON)
                  .content(asJsonString(new MyRequest(1, 2,))))
                  .andExpect(status().is2xxSuccessful())
                  .andExpect(content().contentType(&quot;application/json&quot;))
                  //.andExpect(jsonPath(&quot;@.res1&quot;).value(1L))
                  .andReturn(); 
  
  
          .
          .
          . 
      }
  }
  </code></pre>
  <p>The problem is my request receive directly by rest controller and I can't test the DispatcherServlet.
  I'm using spring boot 2.2.3 with jar packaging and I don't have web.xml or any servlet configration.</p>