title: I/O error on GET "Connection refused" during unit test of custom Spring endpoint that hits a default swagger endpoint in a Spring controller
body: <p>I have an endpoint that comes from a gradle dependency, lets say its <code>/v3/doc</code></p>
  
  <p>I needed to expose an endpoint, lets call it, <code>/v1/info</code> that hits /v3/doc internally, massages, some of the data that comes out, then returns that piece of data to whomever called /v1/info</p>
  
  <p>I wrote the code for it (below i am omitting the massaging part for simplicity), but the unit tests throw the error described during test execution. </p>
  
  <p>My endpoint looks like this:</p>
  
  <pre><code>@RestController
  public class MyController {
  
      @GetMapping("v1/info")
      public @ResponseBody SomePojo swag(HttpServletRequest request) {
          String s = retrieve(request
                                  .getRequestURL()
                                  .toString()
                                  .split("v1")[0] // get the base URL and tack on what i need
                                  .concat("v3/doc")
                          );
  
          return new Gson().fromJson(s, SomePojo.class)
      }
  
      private String retrieve(String url) {
          return new RestTemplate()
              .getForEntity(url, String.class)
              .getBody();
      }
  }
  </code></pre>
  
  <p>unit test looks like:</p>
  
  <pre><code>@WebMvcTest(MyController.class)
  @ActiveProfiles(profiles = "local")
  public class MyControllerTest {
  
  @Autowired
  private MockMvc mockMvc;
  
  @Test
  public void testEndpoint() throws Exception {
      mockMvc.perform(get("/v1/info")
              .contentType(MediaType.APPLICATION_JSON)
              .characterEncoding("utf-8"))
              .andExpect(status().isOk());
  }
  }
  </code></pre>
  
  <p>error looks like:</p>
  
  <p><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost/v3/doc": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)</code></p>
  
  <p>im wondering if it has something to do with the port?? because when i run the app on 8001 i can hit <code>/v3/doc</code> and <code>/v1/info</code> no problem. But as you can see when the unit tests run, there is no port, just localhost. I didnt think that mattered though. Will i have to create some kind of fake mvc stub?</p>